将 QMainWindow 与 Qdialog 小部件连接起来
Posted
技术标签:
【中文标题】将 QMainWindow 与 Qdialog 小部件连接起来【英文标题】:Connect QMainWindow with Qdialog Widgets 【发布时间】:2017-11-12 02:06:32 【问题描述】:首先;感谢您的回答!
我创建了两个不同的窗口和两个不同的 Python 文件。我可以这样连接它们:
当我单击 QlistWidget 项下的“新建项目”按钮时,它会打开。这很好,但我遇到了问题。我想将 QlineEdit(在 QDialogWindow 中)连接到 QlistWidget(在 QMainWindow 中)。
这是我的主窗口代码:
from PySide.QtCore import *
from PySide.QtGui import *
import os, sys
import neworkdialog
class projectManager(QMainWindow, QObject):
def __init__(self):
super(projectManager, self).__init__()
self.managerUI()
def managerUI(self):
def generalClose():
sys.exit()
def itemListLabel():
itemChanged = self.workList.currentItem().text()
self.projectDescription.setText(itemChanged)
def newProject():
neworkdialog.start_nework()
self.menubar = QMenuBar()
self.fileMenu = self.menubar.addMenu("&File")
nw = self.fileMenu.addAction("New Work")
nw.setShortcut("ctrl+n")
nw.triggered.connect(newProject)
self.fileMenu.addAction("Delete Work")
self.fileMenu.addSeparator()
op = self.fileMenu.addAction("Open")
op.setShortcut("ctrl+o")
sv = self.fileMenu.addAction("Save")
sv.setShortcut("ctrl+s")
cs = self.fileMenu.addAction("Close")
cs.setShortcut("ctrl+q")
cs.triggered.connect(generalClose)
self.toolMenu = self.menubar.addMenu("&Tools")
self.toolMenu.addAction("Calculator")
self.toolMenu.addAction("Deadline(s)")
self.aboutMenu = self.menubar.addMenu("&Help")
self.aboutMenu.addAction("About")
self.aboutMenu.addAction("GameTime")
self.aboutMenu.addAction("Help")
self.setMenuBar(self.menubar)
self.workList = QListWidget()
# i = ["deneme1","bdeneme2","adeneme3"]
# self.workList.addItems(i)
self.workList.setMaximumWidth(275)
self.newProjectButton = QPushButton()
self.newProjectButton.setText("New Project")
self.newProjectButton.clicked.connect(newProject)
self.delProjectButton = QPushButton()
self.delProjectButton.setText("Delete Project")
self.projectDescription = QLabel()
self.projectDescription.setText("Project Title")
self.workList.itemClicked.connect(itemListLabel)
self.projectDescription.setStyleSheet("font: 22pt")
self.widgets = QWidget()
self.mainLayout = QHBoxLayout()
self.workListLayout = QVBoxLayout()
self.workListLayout.addWidget(self.workList)
self.projectDescriptionLayout = QHBoxLayout()
self.projectDescriptionLayout.addWidget(self.projectDescription)
self.workButtonLayout = QHBoxLayout()
self.workButtonLayout.addWidget(self.newProjectButton)
self.workButtonLayout.addWidget(self.delProjectButton)
self.workListLayout.addLayout(self.workButtonLayout)
self.mainLayout.addLayout(self.workListLayout)
self.mainLayout.addLayout(self.projectDescriptionLayout)
self.widgets.setLayout(self.mainLayout)
self.setCentralWidget(self.widgets)
def start():
app = QApplication(sys.argv)
ui = projectManager()
ui.setWindowTitle("Projects Manager v.01")
ui.setFixedSize(1280, 920)
ui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
start()
这是我的 Qdialog 代码:
from PySide.QtCore import *
from PySide.QtGui import *
import os, sys
import _projectMainWindow
class neworkdialogs (QDialog, QObject):
def __init__(self):
super(neworkdialogs, self).__init__()
self.dialogBuildUI()
def dialogBuildUI(self):
print _projectMainWindow
def setProjectTitle():
setWindowText = self.projectName.text()
self.setWindowTitle(setWindowText)
def generalExit():
sys.exit()
self.projectName = QLineEdit()
self.projectName.setPlaceholderText("Set Project Name")
self.projectName.textChanged.connect(setProjectTitle)
self.projectPrice = QLineEdit()
self.projectPrice.setPlaceholderText("Set Money Count")
def addNewWrokToList():
ProjectNameOfText = self.projectName.text()
self.worklist.addItem(ProjectNameOfText)
priceItems = ["TL", "USD", "EUR"]
self.priceDoviz = QComboBox()
self.priceDoviz.addItems(priceItems)
self.AcceptButton = QPushButton("Create Work")
self.AcceptButton.clicked.connect(addNewWrokToList)
self.abondon = QPushButton("Abondon")
self.abondon.clicked.connect(generalExit)
self.dialogLayout = QVBoxLayout()
self.inputLayout = QHBoxLayout()
self.buttonLayout = QHBoxLayout()
self.inputLayout.addWidget(self.projectName)
self.inputLayout.addWidget(self.projectPrice)
self.inputLayout.addWidget(self.priceDoviz)
self.dialogLayout.addLayout(self.inputLayout)
self.dialogLayout.addLayout(self.buttonLayout)
self.buttonLayout.addWidget(self.AcceptButton)
self.buttonLayout.addWidget(self.abondon)
self.setLayout(self.dialogLayout)
def start_nework():
ui = neworkdialogs()
ui.setWindowTitle("Your job properties")
ui.show()
ui.exec_()
那么,如何连接不同的窗口和小部件? 再次感谢你:)
【问题讨论】:
所以,我想在不同的 python 文件的不同窗口中连接不同的小部件。 对不起,我的错。我想当我在 QlineEdit 中写一些东西时,它可以附加 qlistWidget。这两个小部件有不同的窗口和不同的python文件。 是的 :) 绝对。 没关系。但它可以是 self.projectName。 【参考方案1】:函数 start_nework 不适用于这种情况,因为无法访问对话框,正确的做法是在按下按钮时创建一个neworkdialogs 实例。 QDialog 旨在处理接受和拒绝功能,以便之后状态返回,即是否接受请求,为此我修改了您的类:
neworkdialog.py
from PySide.QtGui import *
class neworkdialogs(QDialog):
def __init__(self, *args, **kwargs):
super(neworkdialogs, self).__init__(*args, **kwargs)
self.dialogBuildUI()
def dialogBuildUI(self):
self.projectName = QLineEdit(self)
self.projectName.setPlaceholderText("Set Project Name")
self.projectName.textChanged.connect(self.setWindowTitle)
self.projectPrice = QLineEdit(self)
self.projectPrice.setPlaceholderText("Set Money Count")
priceItems = ["TL", "USD", "EUR"]
self.priceDoviz = QComboBox(self)
self.priceDoviz.addItems(priceItems)
self.AcceptButton = QPushButton("Create Work")
self.abondon = QPushButton("Abondon")
self.AcceptButton.clicked.connect(self.accept)
self.abondon.clicked.connect(self.reject)
self.dialogLayout = QVBoxLayout(self)
self.inputLayout = QHBoxLayout()
self.buttonLayout = QHBoxLayout()
self.inputLayout.addWidget(self.projectName)
self.inputLayout.addWidget(self.projectPrice)
self.inputLayout.addWidget(self.priceDoviz)
self.dialogLayout.addLayout(self.inputLayout)
self.dialogLayout.addLayout(self.buttonLayout)
self.buttonLayout.addWidget(self.AcceptButton)
self.buttonLayout.addWidget(self.abondon)
调用sys.exit()
退出窗口是不合适的,正确的做法是使用close()
方法。我还实现了将文本添加到QListWidget
的逻辑。
main.py
from PySide.QtGui import *
import neworkdialog
class projectManager(QMainWindow):
def __init__(self, *args, **kwargs):
super(projectManager, self).__init__(*args, **kwargs)
self.managerUI()
def managerUI(self):
self.menubar = QMenuBar(self)
self.fileMenu = self.menubar.addMenu("&File")
nw = self.fileMenu.addAction("New Work")
nw.setShortcut("ctrl+n")
nw.triggered.connect(self.start_nework)
self.fileMenu.addAction("Delete Work")
self.fileMenu.addSeparator()
op = self.fileMenu.addAction("Open")
op.setShortcut("ctrl+o")
sv = self.fileMenu.addAction("Save")
sv.setShortcut("ctrl+s")
cs = self.fileMenu.addAction("Close")
cs.setShortcut("ctrl+q")
cs.triggered.connect(self.close)
self.toolMenu = self.menubar.addMenu("&Tools")
self.toolMenu.addAction("Calculator")
self.toolMenu.addAction("Deadline(s)")
self.aboutMenu = self.menubar.addMenu("&Help")
self.aboutMenu.addAction("About")
self.aboutMenu.addAction("GameTime")
self.aboutMenu.addAction("Help")
self.setMenuBar(self.menubar)
self.workList = QListWidget(self)
# i = ["deneme1","bdeneme2","adeneme3"]
# self.workList.addItems(i)
self.workList.setMaximumWidth(275)
self.newProjectButton = QPushButton(self)
self.newProjectButton.setText("New Project")
self.newProjectButton.clicked.connect(self.start_nework)
self.delProjectButton = QPushButton(self)
self.delProjectButton.setText("Delete Project")
self.projectDescription = QLabel("Project Title", self)
self.workList.itemClicked.connect(lambda item: self.projectDescription.setText(item.text()))
self.projectDescription.setStyleSheet("font: 22pt")
self.widgets = QWidget()
self.mainLayout = QHBoxLayout(self.widgets)
self.workListLayout = QVBoxLayout()
self.workListLayout.addWidget(self.workList)
self.workButtonLayout = QHBoxLayout()
self.workButtonLayout.addWidget(self.newProjectButton)
self.workButtonLayout.addWidget(self.delProjectButton)
self.workListLayout.addLayout(self.workButtonLayout)
self.mainLayout.addLayout(self.workListLayout)
self.mainLayout.addWidget(self.projectDescription)
self.setCentralWidget(self.widgets)
def start_nework(self):
w = neworkdialog.neworkdialogs(self)
if w.exec_() == QDialog.Accepted:
name = w.projectName.text()
self.workList.addItem(name)
def start():
import sys
app = QApplication(sys.argv)
ui = projectManager()
ui.setWindowTitle("Projects Manager v.01")
ui.setFixedSize(1280, 920)
ui.show()
sys.exit(app.exec_())
if __name__ == '__main__':
start()
【讨论】:
非常感谢。很高兴见到你。 :) 可以吗? :)以上是关于将 QMainWindow 与 Qdialog 小部件连接起来的主要内容,如果未能解决你的问题,请参考以下文章
如何从QDialog刷新/更新/重绘QMainWindow?
PyQt 在 QMainWindow 中创建 QDialog
在打开 qdialog 的情况下关闭 qmainwindow 时 Python 崩溃
QWidget,QMainWindow,QDialog和QFrame的区别