将我的 Qdialog 更改为 QMainWindow
Posted
技术标签:
【中文标题】将我的 Qdialog 更改为 QMainWindow【英文标题】:Change my Qdialog to QMainWindow 【发布时间】:2014-06-23 20:15:06 【问题描述】:基于一些随机示例,我破解了这个漂亮的窗口。我很遗憾使用QDialog
示例而不是QMainWindow
。我喜欢QMainWindow
的外观及其功能。现在我无法进行转换(纯粹的无知)。代码变得相当长,所以我想我会从QMainWindow
调用这段代码,但是当我这样做时,会发生一些灾难,导致所有 python 都关闭。我可以将其转换为QMainWindow
或从QMainWindow
调用它吗?
import sys
from PyQt4 import QtCore, QtGui
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.resize(900,600)
layout = QtGui.QHBoxLayout(self)
Right = QtGui.QVBoxLayout(self)
Left = QtGui.QVBoxLayout(self)
spacer = QtGui.QSpacerItem(20,40)
spacer2 = QtGui.QSpacerItem(10,40)
font = QtGui.QFont()
font.setFamily("Rod")
font.setPointSize(23)
font2 = QtGui.QFont()
font2.setFamily("Rod")
font2.setPointSize(18)
font3 = QtGui.QFont()
font3.setFamily("Rod")
font3.setPointSize(13)
#Plotting Methods from Matplotlib
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.canvas.setMaximumHeight(525)
self.canvas.setMaximumHeight(725)
self.toolbar = NavigationToolbar(self.canvas, self)
#Labels
self.label = QtGui.QLabel(self)
self.label.setFont(font)
self.label.setText(" Welcome to PPMS Data Analyser")
self.label.setMaximumSize(625,30)
label2 = QtGui.QLabel(self)
label2.setFont(font2)
label2.setText("Select Plot:")
x_label = QtGui.QLabel(self)
x_label.setFont(font3)
x_label.setText("x axis:")
y_label = QtGui.QLabel(self)
y_label.setFont(font3)
y_label.setText("y axis:")
#Push Buttons
fileSelect = QtGui.QPushButton('Select File')
fileSelect.setMaximumSize(70,25)
fileSelect.clicked.connect(self.plot)
goPlot = QtGui.QPushButton('Plot')
goPlot.setMaximumSize(60,20)
goPlot.clicked.connect(self.plot)
# x axis radio buttons
groupBox = QtGui.QGroupBox()
radio1 = QtGui.QRadioButton("Time")
radio2 = QtGui.QRadioButton("Delta T")
radio3 = QtGui.QRadioButton("Delta V")
radioa = QtGui.QRadioButton("Hot Side Temp")
radiob = QtGui.QRadioButton("Cold Side Temp")
radioc = QtGui.QRadioButton("System Temp")
radio2.setChecked(True)
# Vertically aligns these buttons
vbox = QtGui.QVBoxLayout()
vbox.addWidget(x_label)
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addWidget(radio3)
vbox.addWidget(radioa)
vbox.addWidget(radiob)
vbox.addWidget(radioc)
vbox.addStretch(1)
groupBox.setLayout(vbox)
# y axis radio buttons
groupBox2 = QtGui.QGroupBox()
radio12 = QtGui.QRadioButton("Time")
radio22 = QtGui.QRadioButton("Delta T")
radio32 = QtGui.QRadioButton("Delta V")
radioa2 = QtGui.QRadioButton("Hot Side Temp")
radiob2 = QtGui.QRadioButton("Cold Side Temp")
radioc2 = QtGui.QRadioButton("System Temp")
radio32.setChecked(True)
# Vertically aligns these buttons
vbox2 = QtGui.QVBoxLayout()
vbox2.addWidget(y_label)
vbox2.addWidget(radio12)
vbox2.addWidget(radio22)
vbox2.addWidget(radio32)
vbox2.addWidget(radioa2)
vbox2.addWidget(radiob2)
vbox2.addWidget(radioc2)
vbox2.addStretch(1)
groupBox2.setLayout(vbox2)
#Box for 2 radio boxes
hbox0 = QtGui.QHBoxLayout()
hbox0.addWidget(groupBox)
hbox0.addWidget(groupBox2)
hbox0.addStretch(0)
Left.addWidget(self.label)
Left.addWidget(self.canvas)
Left.addWidget(self.toolbar)
Right.addItem(spacer)
Right.addWidget(fileSelect)
Right.addItem(spacer)
Right.addWidget(label2)
Right.addLayout(hbox0)
Right.addWidget(goPlot)
Right.addLayout(hbox0)
layout.addLayout(Left)
layout.addLayout(Right)
def plot(self):
ax = self.figure.add_subplot(111)
ax.hold(False)
ax.plot([1,2],[3,4])
plt.grid()
plt.title('Test Plot')
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.setWindowTitle('PPMS Seebeck Calculator')
main.show()
sys.exit(app.exec_())
【问题讨论】:
小错误,但您的代码对我有用(Python 3.3) 代码运行良好,但我希望将这个 QDialog 程序更改为与 QMainWindow 一起使用的程序。 【参考方案1】:QMainWindow
有一个预设布局 [docs]。首先,您需要为QMainWindow
创建一个“central widget”
,并将您自己的布局添加到该小部件。这是工作代码:
import sys
from PyQt4 import QtCore, QtGui
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.resize(900,600)
self.central_widget = QtGui.QWidget()
self.setCentralWidget(self.central_widget)
layout = QtGui.QHBoxLayout()
Right = QtGui.QVBoxLayout()
Left = QtGui.QVBoxLayout()
spacer = QtGui.QSpacerItem(20,40)
spacer2 = QtGui.QSpacerItem(10,40)
font = QtGui.QFont()
font.setFamily("Rod")
font.setPointSize(23)
font2 = QtGui.QFont()
font2.setFamily("Rod")
font2.setPointSize(18)
font3 = QtGui.QFont()
font3.setFamily("Rod")
font3.setPointSize(13)
#Plotting Methods from Matplotlib
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.canvas.setMaximumHeight(525)
self.canvas.setMaximumHeight(725)
self.toolbar = NavigationToolbar(self.canvas, self)
#Labels
self.label = QtGui.QLabel(self)
self.label.setFont(font)
self.label.setText(" Welcome to PPMS Data Analyser")
self.label.setMaximumSize(625,30)
label2 = QtGui.QLabel(self)
label2.setFont(font2)
label2.setText("Select Plot:")
x_label = QtGui.QLabel(self)
x_label.setFont(font3)
x_label.setText("x axis:")
y_label = QtGui.QLabel(self)
y_label.setFont(font3)
y_label.setText("y axis:")
#Push Buttons
fileSelect = QtGui.QPushButton('Select File')
fileSelect.setMaximumSize(70,25)
fileSelect.clicked.connect(self.plot)
goPlot = QtGui.QPushButton('Plot')
goPlot.setMaximumSize(60,20)
goPlot.clicked.connect(self.plot)
# x axis radio buttons
groupBox = QtGui.QGroupBox()
radio1 = QtGui.QRadioButton("Time")
radio2 = QtGui.QRadioButton("Delta T")
radio3 = QtGui.QRadioButton("Delta V")
radioa = QtGui.QRadioButton("Hot Side Temp")
radiob = QtGui.QRadioButton("Cold Side Temp")
radioc = QtGui.QRadioButton("System Temp")
radio2.setChecked(True)
# Vertically aligns these buttons
vbox = QtGui.QVBoxLayout()
vbox.addWidget(x_label)
vbox.addWidget(radio1)
vbox.addWidget(radio2)
vbox.addWidget(radio3)
vbox.addWidget(radioa)
vbox.addWidget(radiob)
vbox.addWidget(radioc)
vbox.addStretch(1)
groupBox.setLayout(vbox)
# y axis radio buttons
groupBox2 = QtGui.QGroupBox()
radio12 = QtGui.QRadioButton("Time")
radio22 = QtGui.QRadioButton("Delta T")
radio32 = QtGui.QRadioButton("Delta V")
radioa2 = QtGui.QRadioButton("Hot Side Temp")
radiob2 = QtGui.QRadioButton("Cold Side Temp")
radioc2 = QtGui.QRadioButton("System Temp")
radio32.setChecked(True)
# Vertically aligns these buttons
vbox2 = QtGui.QVBoxLayout()
vbox2.addWidget(y_label)
vbox2.addWidget(radio12)
vbox2.addWidget(radio22)
vbox2.addWidget(radio32)
vbox2.addWidget(radioa2)
vbox2.addWidget(radiob2)
vbox2.addWidget(radioc2)
vbox2.addStretch(1)
groupBox2.setLayout(vbox2)
#Box for 2 radio boxes
hbox0 = QtGui.QHBoxLayout()
hbox0.addWidget(groupBox)
hbox0.addWidget(groupBox2)
hbox0.addStretch(0)
Left.addWidget(self.label)
Left.addWidget(self.canvas)
Left.addWidget(self.toolbar)
Right.addItem(spacer)
Right.addWidget(fileSelect)
Right.addItem(spacer)
Right.addWidget(label2)
Right.addLayout(hbox0)
Right.addWidget(goPlot)
#Right.addLayout(hbox0)
layout.addLayout(Left)
layout.addLayout(Right)
self.central_widget.setLayout(layout)
def plot(self):
ax = self.figure.add_subplot(111)
ax.hold(False)
ax.plot([1,2],[3,4])
plt.grid()
plt.title('Test Plot')
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.setWindowTitle('PPMS Seebeck Calculator')
main.show()
sys.exit(app.exec_())
这就是我所做的:
1) 创建central_widget
:
self.central_widget = QtGui.QWidget()
self.setCentralWidget(self.central_widget)
2)为了避免这个警告:QLayout: Attempting to add QLayout "" to Window "", which already has a layout
我改变了
layout = QtGui.QHBoxLayout(self)
Right = QtGui.QVBoxLayout(self)
Left = QtGui.QVBoxLayout(self)
到
layout = QtGui.QHBoxLayout()
Right = QtGui.QVBoxLayout()
Left = QtGui.QVBoxLayout()
并评论了第二个Right.addLayout(hbox0)
3) central_widget
的设置布局:self.central_widget.setLayout(layout)
【讨论】:
非常感谢。这是完美的!以上是关于将我的 Qdialog 更改为 QMainWindow的主要内容,如果未能解决你的问题,请参考以下文章
我可以随时将我的应用程序从免费更改为付费或从付费更改为免费吗?