QApplication框架内的PyQt5 QDialog

时间:2018-08-27 18:55:01

标签: python-3.x pyqt5

在选择适合我所需要方法的过程中,有些困惑。我有一个PyQt5应用程序,需要为其弹出QDialog(以显示进度):

import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QWidget, QDialog
from PyQt5.uic import loadUi


class TestWidget(QDialog):
    def __init__(self,*args):
        super(TestWidget,self).__init__(*args)
        loadUi('test2.ui',self)

class ClientReports(QMainWindow):
    def __init__(self,*args):
        super(ClientReports,self).__init__(*args)
        loadUi('clientreports.ui',self)
        self.subWindowCounter = 0
        testItem = QAction('&Test',self)
        testItem.setShortcut('Ctrl+T')
        testItem.setStatusTip('Test The New Process')
        testItem.triggered.connect(self.test_new_item)
        self.menubar.addAction(testItem)

    def test_new_item(self):
        tw = TestWidget(self)
        tw.exec()

app = QApplication(sys.argv)
mainWin = ClientReports()
mainWin.show()
sys.exit(app.exec_())

我的问题是:使用QDialog小部件可以使用户在整个屏幕上拖动窗口,而实际上,我需要停留在应用程序范围内。输入QMdiSubMenu选项-保留在应用程序框架的范围内,但我不能使其成为模态的。关于如何完成创建模态窗口但仍在应用程序框架范围内的任何建议?

clientreports.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>mainWindow</class>
 <widget class="QMainWindow" name="mainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1118</width>
    <height>730</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>PPS Client Custom Reports</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <property name="leftMargin">
     <number>0</number>
    </property>
    <property name="topMargin">
     <number>0</number>
    </property>
    <property name="rightMargin">
     <number>0</number>
    </property>
    <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <widget class="QMdiArea" name="mdiArea"/>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1118</width>
     <height>23</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuReports">
    <property name="title">
     <string>Reports</string>
    </property>
    <addaction name="actionExit"/>
   </widget>
   <addaction name="menuReports"/>
  </widget>
  <action name="actionExit">
   <property name="text">
    <string>Exit</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

test2.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>114</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QProgressBar" name="progressBar">
     <property name="value">
      <number>24</number>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

enter image description here

1 个答案:

答案 0 :(得分:-1)

  

Qt :: FramelessWindowHint
  产生一个无边界的窗口。用户无法通过窗口系统移动或调整无边界窗口的大小。在X11上,标志的结果取决于窗口管理器及其理解Motif和/或NETWM提示的能力。现有的大多数现代窗口管理器都可以解决这个问题。

import sys
from PyQt5.QtGui     import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QWidget, QDialog
from PyQt5.uic       import loadUi
from PyQt5.QtCore    import Qt


class TestWidget(QDialog):
    def __init__(self,*args):
        super(TestWidget,self).__init__(*args)
        loadUi('test2.ui',self)
        # +++
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

class ClientReports(QMainWindow):
    def __init__(self,*args):
        super(ClientReports,self).__init__(*args)
        loadUi('clientreports.ui',self)
        self.subWindowCounter = 0
        testItem = QAction('&Test',self)
        testItem.setShortcut('Ctrl+T')
        testItem.setStatusTip('Test The New Process')
        testItem.triggered.connect(self.test_new_item)
        self.menubar.addAction(testItem)

    def test_new_item(self):
        tw = TestWidget(self)
        tw.exec()

app = QApplication(sys.argv)
mainWin = ClientReports()
mainWin.show()
sys.exit(app.exec_())

enter image description here