我正在用PyQt开发一个图形用户界面应用程序。
对于按钮,我使用了QWidget,对于QSqlTable,我使用了QMainWindow。
我知道我可以通过在QWidget中添加setCentralwidget来添加QMainWindow。但是为了显示表格,我需要表格作为中心小部件。
两个都可以单独使用,但我无法同时拥有这两个。
我的代码如下:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QVBoxLayout, QGroupBox, QGridLayout, QPushButton,QLabel.
from PyQt5.QtGui import QPixmap
from PyQt5 import QtGui
from gwWindow import groundwaterWindow
from swWindow import surfacewaterWindow
from kwWindow import karstwaterWindow
from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox, QTableView, QPushButton, QGridLayout, QGroupBox, QVBoxLayout)
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSignal
class MainWindow(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.title = "PPCP Selection"
self.top = 100
self.left = 100
self.width = 1200
self.height = 1200
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.createLayout()
vBoxLayout = QVBoxLayout()
vBoxLayout.addWidget(self.groupBox1)
# vBoxLayout.addWidget(self.groupBox2)
self.setLayout(vBoxLayout)
# self.showMaximized()
def createLayout(self):
self.groupBox1 = QGroupBox("PPCP Selection")
self.groupBox1.setFont(QtGui.QFont("Arial", 30))
gridLayout = QGridLayout()
gridLayout.setRowStretch(2, 2)
self.button1 = QPushButton("Evaluate")
gridLayout.addWidget(self.button1, 1, 0, 1, 1)
#self.button1.clicked.connect(self.switch1)
gridLayout.addWidget(self.button1)
self.button1.setFocusPolicy(QtCore.Qt.NoFocus)
self.groupBox1.setLayout(gridLayout)
class RealMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("QTableView Example")
self.resize(415, 200)
# Set up the model
self.model = TableModel(self)
self.model.setTable("Table2")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
#self.model.setHeaderData(0, Qt.Horizontal, "ID")
self.model.select()
# Set up the view
self.view = QTableView()
self.view.setModel(self.model)
#self.view.resizeColumnsToContents()
self.another = MainWindow()
# I want both of the following
# self.setCentralWidget(self.view)
# self.setCentralWidget(self.another)
def secondWindow(self):
self.another = MainWindow()
self.another.show()
class TableModel(QSqlTableModel):
def __init__(self, *args, **kwargs):
QSqlTableModel.__init__(self, *args, **kwargs)
self.checkeable_data = {}
def flags(self, index):
fl = QSqlTableModel.flags(self, index)
if index.column() == 0:
fl |= Qt.ItemIsUserCheckable
return fl
def data(self, index, role=Qt.DisplayRole):
if role == Qt.CheckStateRole and (
self.flags(index) & Qt.ItemIsUserCheckable != Qt.NoItemFlags
):
if index.row() not in self.checkeable_data.keys():
self.setData(index, Qt.Unchecked, Qt.CheckStateRole)
return self.checkeable_data[index.row()]
else:
return QSqlTableModel.data(self, index, role)
def setData(self, index, value, role=Qt.EditRole):
if role == Qt.CheckStateRole and (
self.flags(index) & Qt.ItemIsUserCheckable != Qt.NoItemFlags
):
self.checkeable_data[index.row()] = value
self.dataChanged.emit(index, index, (role,))
return True
return QSqlTableModel.setData(self, index, value, role)
def createConnection():
con = QSqlDatabase.addDatabase("QSQLITE")
con.setDatabaseName("ppcp_database.db")
if not con.open():
QMessageBox.critical(
None,
"QTableView Example - Error!",
"Database Error: %s" % con.lastError().databaseText(),
)
return False
return True
app = QApplication(sys.argv)
if not createConnection():
sys.exit(1)
win = RealMainWindow()
win.show()
sys.exit(app.exec_())
发布于 2021-03-15 18:45:14
使用Qt布局:
class RealMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("QTableView Example")
self.resize(415, 200)
# Set up the model
self.model = TableModel(self)
self.model.setTable("Table2")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.select()
self.view = QTableView()
self.view.setModel(self.model)
self.another = MainWindow()
central_widget = QWidget()
self.setCentralWidget(central_widget)
lay = QVBoxLayout(central_widget)
lay.addWidget(self.view)
lay.addWidget(self.another)
https://stackoverflow.com/questions/66643206
复制相似问题