01/10/2018, 11:47
Điều khiển button để đưa dữ liệu vào table
Em đang làm app kiểu như vậy, khi nhấn RUN thì hiện lên cái bảng, và cái nhấn nút CHECK thì lấy dữ liệu ở x1= QLineEdit() đưa vào table.
Ở RUN em cho chạy:
LayoutWidget = FirstWidget()
self.setCentralWidget(LayoutWidget)
self.show()
Còn CHECK em chưa biết điều khiển nó như nào, vì table ở class FirstWidget(QWidget):, còn CHECK lại ở class Form(QMainWindow):, khi viết code cho check thì nó không hiểu table.
Giờ em phải làm như nào ạ

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import numpy as np
class Form(QMainWindow):
def __init__(self):
super(Form, self).__init__()
screenSize = QDesktopWidget().screenGeometry()
width, height = screenSize.width(), screenSize.height()
self.setGeometry(width/2-300, height/2-250, 600, 500)
self.setWindowTitle("Classification of Ceramic Tiles")
self.setWindowIcon(QIcon("icon.png"))
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
editMenu = mainMenu.addMenu('&Edit')
InsertAction = QAction('&Insert',self)
InsertAction.setStatusTip("Insert")
InsertAction.setShortcut('Ctrl+N')
RunAction = QAction(QIcon('run.png'),'&Run', self)
RunAction.setStatusTip("Run")
RunAction.setShortcut('Ctrl+R')
RunAction.triggered.connect(self.runbutton)
# self.RunAction.clicked.connect(self.runbutton)
DeleteAction = QAction(QIcon('delete.png'),'&Delete', self)
DeleteAction.setStatusTip("Delete")
DeleteAction.setShortcut('Ctrl+D')
ExitAction = QAction(QIcon('exit.png'), '&Exit', self)
ExitAction.setStatusTip("Exit")
ExitAction.setShortcut('Ctrl+E')
ExitAction.triggered.connect(self.close)
CheckAction = QAction(QIcon('check.png'), '&Check', self)
CheckAction.setStatusTip("Check")
CheckAction.setShortcut('Ctrl+T')
CheckAction.triggered.connect(self.checkbutton)
fileMenu.addAction(InsertAction)
fileMenu.addAction(RunAction)
fileMenu.addAction(DeleteAction)
fileMenu.addAction(CheckAction)
fileMenu.addAction(ExitAction)
self.statusBar()
toolBox = self.addToolBar("ToolP")
toolBox.addAction(RunAction)
toolBox.addAction(DeleteAction)
toolBox.addAction(CheckAction)
toolBox.addAction(ExitAction)
# LayoutWidget = FirstWidget()
# self.setCentralWidget(LayoutWidget)
self.show()
def runbutton(self):
LayoutWidget = FirstWidget()
self.setCentralWidget(LayoutWidget)
self.show()
def checkbutton(self):
table.setItem(0, 0, QTableWidgetItem(x1))
class FirstWidget(QWidget):
def __init__(self):
super(FirstWidget, self).__init__()
table = QTableWidget()
tableItem = QTableWidgetItem()
formLayout = QFormLayout()
table.setRowCount(10)
table.setColumnCount(3)
x1 = QLineEdit()
y1 = QLineEdit()
h475 = QLabel("475Hz")
h555 = QLabel("555Hz")
formLayout.addRow(h475, x1)
formLayout.addRow(h555, y1)
titleHeader = ["Value At 475Hz", "Value At 555Hz", "Qualities"]
table.setHorizontalHeaderLabels(titleHeader)
header = table.horizontalHeader()
header.setResizeMode(QHeaderView.Stretch)
vbox = QVBoxLayout()
vbox.addWidget(table)
formLayout.addRow(vbox)
self.setLayout(formLayout)
app = QApplication(sys.argv)
GUI = Form()
sys.exit(app.exec_())
Bài liên quan
Tạo một thuộc tính ở lớp
Form
giữ giá trị là một đối tượng thuộc lớpFirstWidget
.Kiểu
Rồi lấy item chẳng hạn
Chuẩn rồi, em vừa ra sáng nay.
Cảm ơn bạn nhiều nha