在 PyQt 中已有中央小部件的 QMainWindow 中添加 QWidget
Adding QWidget in QMainWindow which already has a central widget in PyQt
我正在用 PyQt 开发一个 GUI 应用程序。
对于按钮,我使用了 QWidget,对于 QSqlTable,我使用了 QMainWindow。
我知道我可以通过在 QMainWindow 中添加 setCentralwidget 来添加 QWidget。
但是为了显示 Table,我需要 table 作为中心小部件。
单独两个都可以,但我无法同时拥有两个。
我的代码如下:
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_())
使用 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)
我正在用 PyQt 开发一个 GUI 应用程序。
对于按钮,我使用了 QWidget,对于 QSqlTable,我使用了 QMainWindow。
我知道我可以通过在 QMainWindow 中添加 setCentralwidget 来添加 QWidget。 但是为了显示 Table,我需要 table 作为中心小部件。
单独两个都可以,但我无法同时拥有两个。
我的代码如下:
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_())
使用 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)