将QMainWindow与Qdialog Widgets连接

时间:2017-11-12 02:06:33

标签: python pyside qmainwindow qdialog

首先关闭所有;谢谢你的回答!

我创建了两个不同的窗口和两个不同的Python文件。我可以像这样连接它们:

img

当我点击" New Project" QlistWidget项下的按钮然后打开。这很好,但我有一个问题。我想将QlineEdit(在QDialogWindow中)连接到QlistWidget(在QMainWindow中)。

这是我的MainWindow代码:

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_()

那么,我如何连接不同的窗口和小部件? 再次感谢你:)

1 个答案:

答案 0 :(得分:0)

函数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()