将 QMainWindow 与 Qdialog 小部件连接起来
Connect QMainWindow with Qdialog Widgets
首先;感谢您的回答!
我创建了两个不同的 windows 和两个不同的 Python 文件。我可以这样连接它们:
当我单击 QlistWidget 项下的 "New Project" 按钮时,它 opens.This 很好,但我遇到了问题。我想将 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_()
那么,如何连接不同的 window 和小部件?
再次感谢您:)
函数 start_nework 不适用于这种情况,因为无法访问对话框,正确的做法是在按下按钮时创建一个 neworkdialogs 实例。 QDialog是面向处理accept和reject函数的,所以之后的状态是returns,也就是说请求是否被接受,为此我修改了你的class:
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()
退出一个window是不合适的,正确的做法是使用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()
首先;感谢您的回答!
我创建了两个不同的 windows 和两个不同的 Python 文件。我可以这样连接它们:
当我单击 QlistWidget 项下的 "New Project" 按钮时,它 opens.This 很好,但我遇到了问题。我想将 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_()
那么,如何连接不同的 window 和小部件? 再次感谢您:)
函数 start_nework 不适用于这种情况,因为无法访问对话框,正确的做法是在按下按钮时创建一个 neworkdialogs 实例。 QDialog是面向处理accept和reject函数的,所以之后的状态是returns,也就是说请求是否被接受,为此我修改了你的class:
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()
退出一个window是不合适的,正确的做法是使用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()