I wanted to have a total of 8 groupbox in the Dialog box. I don't know how to associate the horizontal scrollbar so that I can scroll it down and access all the group box. In the code below I have added only 2 as an example. Any help is appreciated.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(300, 20, 81, 71))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.scrollArea = QtGui.QScrollArea(Dialog)
self.scrollArea.setGeometry(QtCore.QRect(30, 20, 251, 251))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 249, 249))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.groupBox = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 211, 81))
self.groupBox.setObjectName("groupBox")
self.textEdit = QtGui.QTextEdit(self.groupBox)
self.textEdit.setGeometry(QtCore.QRect(10, 20, 171, 51))
self.textEdit.setObjectName("textEdit")
self.groupBox_2 = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_2.setGeometry(QtCore.QRect(10, 110, 211, 111))
self.groupBox_2.setObjectName("groupBox_2")
self.textEdit_2 = QtGui.QTextEdit(self.groupBox_2)
self.textEdit_2.setGeometry(QtCore.QRect(10, 20, 171, 84))
self.textEdit_2.setObjectName("textEdit_2")
self.verticalScrollBar = QtGui.QScrollBar(self.scrollAreaWidgetContents)
self.verticalScrollBar.setGeometry(QtCore.QRect(230, 0, 16, 241))
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setObjectName("verticalScrollBar")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_2.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
As I said in the comments, QScrollArea doesn't need manual QScrollBar. It will create when needed. I can't be sure what your problem is without seeing the 'not working' code, but my guess is the fixed sized items and their placement. You are probably putting things outside of the widgets margin or overlapping each other so that the inner widget does not grow appropriately.
Anyway, here is a minimal example that replicates your dialog (notice the scrollbar):
import sys
from PyQt4 import QtGui, QtCore
class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
scrolllayout = QtGui.QVBoxLayout()
scrollwidget = QtGui.QWidget()
scrollwidget.setLayout(scrolllayout)
scroll = QtGui.QScrollArea()
scroll.setWidgetResizable(True) # Set to make the inner widget resize with scroll area
scroll.setWidget(scrollwidget)
self.groupboxes = [] # Keep a reference to groupboxes for later use
for i in range(8): # 8 groupboxes with textedit in them
groupbox = QtGui.QGroupBox('%d' % i)
grouplayout = QtGui.QHBoxLayout()
grouptext = QtGui.QTextEdit()
grouplayout.addWidget(grouptext)
groupbox.setLayout(grouplayout)
scrolllayout.addWidget(groupbox)
self.groupboxes.append(groupbox)
self.buttonbox = QtGui.QDialogButtonBox()
self.buttonbox.setOrientation(QtCore.Qt.Vertical)
self.buttonbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
layout = QtGui.QHBoxLayout()
layout.addWidget(scroll)
layout.addWidget(self.buttonbox)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog = MyDialog()
dialog.show()
sys.exit(app.exec_())
Related
I am trying to load my homescreen after the splashscreen. Upon running the code, the splashscreen manages to load but it stops there. How do I fix this?
This is my SplashScreen class
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
This is the function that lets the splashscreen load and launch the WindowApp class but it does not work. What I mean by does not work I meant that it does not load the WindowApp. It just closes.
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
self.counter += 1
This is my main window
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
I have tried changing this to WindowApp() just to check if the MainWindow has any problems but it manages to load without the splashscreen.
app = QApplication(sys.argv)
splash = SplashScreen()
splash.show()
sys.exit(app.exec_())
Check my answer from this previous post that I think will help you.
You can show the splash screen and when the splash screen closes then you can show your main window. It's similar to the login window from this previous question
But if you want to keep implementing your code the way you have it I think I know the problem.
You call a method which creates the QMainWindow and calls the .show() for that QMainWindow. Once the method is done the variables in that method are garbage collected and so is your QMainWindow object. The way to keep the reference and prevent those variables from getting garbage collected is by making it a class variable by adding self. in front of the variable.
I would maybe restructure a bit and do something like this:
if __name__ == "__main__":
app = QApplication(sys.argv)
splash = SplashScreen()
splash_closed = splash.exec_()
if splash_closed:
window = WindowApp()
window.show()
sys.exit(app.exec_())
Where the splashscreen is now a QDialog and when you're done loading you close the dialog with self.accept() instead of trying to show a new window:
class SplashScreen(QDialog):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
...
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
self.accept()
self.counter += 1
And you should also use the __init__ method on your main window and maybe move all the self.setupUI code in there, or just do something like this:
class WindowApp(QMainWindow):
def __init__(self):
QtWidgets.QDialog.__init__(self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
Hope this helps
import sys
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFrame, QLabel, QProgressBar
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt, QTimer
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(700, 350)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 100
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(30)
def initUI(self):
# layout to display splash scrren frame
layout = QVBoxLayout()
self.setLayout(layout)
# splash screen frame
self.frame = QFrame()
layout.addWidget(self.frame)
# splash screen title
self.title_label = QLabel(self.frame)
self.title_label.setObjectName('title_label')
self.title_label.resize(690, 120)
self.title_label.move(0, 5) # x, y
self.title_label.setText('Splash Screen')
self.title_label.setAlignment(Qt.AlignCenter)
# splash screen title description
self.description_label = QLabel(self.frame)
self.description_label.resize(690, 40)
self.description_label.move(0, self.title_label.height())
self.description_label.setObjectName('desc_label')
self.description_label.setText('<b>Splash Screen PyQt-5</b>')
self.description_label.setAlignment(Qt.AlignCenter)
# splash screen pogressbar
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, 180) # self.description_label.y()+130
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
# spash screen loading label
self.loading_label = QLabel(self.frame)
self.loading_label.resize(self.width() - 10, 50)
self.loading_label.move(0, self.progressBar.y() + 70)
self.loading_label.setObjectName('loading_label')
self.loading_label.setAlignment(Qt.AlignCenter)
self.loading_label.setText('Loading...')
self.show()
def loading(self):
# set progressbar value
self.progressBar.setValue(self.counter)
# stop progress if counter
# is greater than n and
# display main window app
if self.counter >= self.n:
self.timer.stop()
'''
self.close()
MainWindow = QtWidgets.QMainWindow()
ui = WindowApp()
ui.setupUi(MainWindow)
MainWindow.show()
'''
self.ui = WindowApp()
self.ui.show()
self.close()
self.counter += 1
class WindowApp(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1006, 654)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 981, 641))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.label.setFont(font)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("../pygui/Homescreen.png"))
self.label.setScaledContents(True)
self.label.setWordWrap(False)
self.label.setObjectName("label")
self.infoBtn = QtWidgets.QPushButton(self.centralwidget)
self.infoBtn.setGeometry(QtCore.QRect(120, 550, 181, 41))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(15)
self.infoBtn.setFont(font)
self.infoBtn.setStyleSheet("border-radius: 10px;\n"
"border-color: 2px solid gray;\n"
"background-color: rgb(255, 255, 255);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/info_icon.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.infoBtn.setIcon(icon)
self.infoBtn.setObjectName("infoBtn")
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setGeometry(QtCore.QRect(310, 270, 431, 111))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(28)
self.startBtn.setFont(font)
self.startBtn.setStyleSheet("background-color: #305E6C;\n"
"color: white;\n"
"border-radius: 10px;\n"
"border: 2px solid white")
self.startBtn.setObjectName("startBtn")
self.contactsBtn = QtWidgets.QPushButton(self.centralwidget)
self.contactsBtn.setGeometry(QtCore.QRect(720, 30, 101, 101))
self.contactsBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.contactsBtn.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/typcn_contacts.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.contactsBtn.setIcon(icon1)
self.contactsBtn.setIconSize(QtCore.QSize(40, 40))
self.contactsBtn.setObjectName("contactsBtn")
self.dateBtn = QtWidgets.QPushButton(self.centralwidget)
self.dateBtn.setGeometry(QtCore.QRect(840, 30, 101, 101))
self.dateBtn.setStyleSheet("\n"
"background-color: #121212;\n"
"border-radius: 50%;\n"
"border: 2px solid white;")
self.dateBtn.setText("")
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/Group.png"),
QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.dateBtn.setIcon(icon2)
self.dateBtn.setIconSize(QtCore.QSize(40, 40))
self.dateBtn.setObjectName("dateBtn")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.infoBtn.clicked.connect(partial(self.show_popup))
self.dateBtn.clicked.connect(self.openDateWindow)
self.startBtn.clicked.connect(self.openDriveWindow)
self.contactsBtn.clicked.connect(self.openContactWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.infoBtn.setText(_translate("MainWindow", "About"))
self.startBtn.setText(_translate("MainWindow", "Driving Mode"))
def show_popup(self):
msg = QMessageBox()
msg.setWindowTitle('About Us')
msg.setText('Testing')
msg.setIcon(QMessageBox.Information)
x = msg.exec_()
def openDateWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DateWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openDriveWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_DriveWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
def openContactWindow(self, MainWindow):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_ContactWindow()
self.ui.setupUi(self.window, MainWindow)
self.window.show()
MainWindow.hide()
app = QApplication(sys.argv)
splash = SplashScreen()
#splash.show()
sys.exit(app.exec_())
I want the click of 'OK' to run some code and then NOT close the dialog
Ui code is as below. I capture the button click using
self.accepted.connect(self.browse_folder)
However, what is happening is that as self.browse_folder is called, the main dialog closes. This behaviour seems to be Related to reject / accept connections in a ButtonBox
from PyQt5 import QtCore, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(600, 400)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(10, 350, 161, 41))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel |
QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 20, 561, 301))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Read A File"))
Try self.close()
class ThermalWindow(QDialog):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window as we want.
"""
def __init__(self,parent,title):
super().__init__()
self.parent = parent
self.resize(400,400)
self.setWindowTitle('Thermal ' + title)
buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
buttonBox = QDialogButtonBox(buttons)
buttonBox.accepted.connect(self.ok_callback)
buttonBox.rejected.connect(self.cancel_callback)
layout = QFormLayout()
self.var = QLineEdit()
layout.addRow(title + " :", self.var)
layout.addWidget(buttonBox)
self.setLayout(layout)
def ok_callback(self):
print("OK")
self.close()
def cancel_callback(self):
print("Cancel")
self.close()
What I'm doing is about that when I clicked the date of the
calendar ,then it will display the class of UI_tab which is a tabwidget ,including two tabs.each tab includes some labels and others.However it
cant't work.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Tab(object):
# create tabwidget
def __init__(self,parentw,*args):
print(args)
print(parentw)
super().__init__()
self.font = QtGui.QFont()
self.font.setFamily("仿宋")
self.font.setPointSize(10)
self.font.setItalic(False)
self.font.setUnderline(False)
self.w=['title0','title1',
'creator0','creator1',
'c_time0','c_time1',
'user0','user1',
'd_time0','d_time1',
'detail0']
self.wt=['title','creator','create_time',
'viewer','finish_time','detail']
self.tabWidget = QtWidgets.QTabWidget(parentw)
self.tabWidget.setGeometry(QtCore.QRect(50, 10, 500, 2500))
self.tabWidget.setMinimumSize(QtCore.QSize(400, 400))
self.tabWidget.setTabsClosable(False)
self.tabWidget.setMovable(True)
self.tabWidget.setObjectName("tabWidget")
self.initTab()
# create tabs in tabwidget
def initTab(self,tabnum=2):
self.tabnum=tabnum
print('inittab')
self.tabs={}
for i in range(self.tabnum):
self.tabs[str(i)]=QtWidgets.QWidget()
self.tabs[str(i)].setObjectName("tab"+str(i))
self.tabWidget.addTab(self.tabs[str(i)], "")
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabs[str(i)]),str(i))
self.initw_in_tab(self.tabs[str(i)])
print(self.tabs)
# add labels,textbrowser in tabs
def initw_in_tab(self,parentw):
w_in_tab={}
print('initw_in_tab')
for i in self.w:
w_in_tab[i]=QtWidgets.QLabel(parentw)
w_in_tab[i].setFont(self.font)
w_in_tab[i].setLayoutDirection(QtCore.Qt.LeftToRight)
w_in_tab[i].setAutoFillBackground(False)
w_in_tab[i].setFrameShape(QtWidgets.QFrame.Box)
w_in_tab[i].setFrameShadow(QtWidgets.QFrame.Raised)
w_in_tab[i].setLineWidth(1)
w_in_tab[i].setMidLineWidth(0)
w_in_tab[i].setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
w_in_tab[i].setIndent(3)
w_in_tab[i].setObjectName(i)
if self.w.index(i)%2 ==0:
w_in_tab[i].setGeometry(QtCore.QRect(20, 180 + self.w.index(i)*25, 59, 25))
w_in_tab[i].setText(self.wt[int(self.w.index(i)/2)])
else:
w_in_tab[i].setGeometry(QtCore.QRect(140, 180 +(self.w.index(i)-1)*25, 59, 25))
scrollArea = QtWidgets.QScrollArea(parentw)
scrollArea.setEnabled(True)
scrollArea.setGeometry(QtCore.QRect(140, 7*25+180, 341, 440))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(scrollArea.sizePolicy().hasHeightForWidth())
scrollArea.setSizePolicy(sizePolicy)
scrollArea.setMinimumSize(QtCore.QSize(200, 100))
scrollArea.setWidgetResizable(True)
scrollArea.setObjectName("scrollArea")
scrollAreaWidgetContents_2 = QtWidgets.QWidget()
scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 322, 1600))
scrollAreaWidgetContents_2.setMinimumSize(QtCore.QSize(200, 1600))
scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
detail2 = QtWidgets.QTextBrowser(scrollAreaWidgetContents_2)
detail2.setGeometry(QtCore.QRect(0, 0, 321, 600))
detail2.setMinimumSize(QtCore.QSize(296, 192))
detail2.setObjectName("detail2")
scrollArea.setWidget(scrollAreaWidgetContents_2)
class Ui_MainWindow(object):
# setup main ui include calendar
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(997, 707)
MainWindow.setStyleSheet("")
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.centralWidget.setUpdatesEnabled(True)
self.calendarw = QtWidgets.QWidget(self.centralWidget)
self.calendarw.setGeometry(QtCore.QRect(560, 20, 361, 541))
self.calendarw.setObjectName("calendarw")
self.cal = QtWidgets.QCalendarWidget(self.calendarw)
self.cal.setGeometry(QtCore.QRect(0, 60, 361, 481))
self.cal.setFirstDayOfWeek(QtCore.Qt.Monday)
self.cal.setObjectName("cal")
self.cal_la = QtWidgets.QLabel(self.calendarw)
self.cal_la.setGeometry(QtCore.QRect(110, 20, 161, 31))
font = QtGui.QFont()
font.setPointSize(22)
self.cal_la.setFont(font)
self.cal_la.setAlignment(QtCore.Qt.AlignCenter)
self.cal_la.setObjectName("cal_la")
MainWindow.setCentralWidget(self.centralWidget)
MainWindow.setWindowTitle("MainWindow")
self.cal_la.setText("calendar")
#
# Ui_Tab(self.centralWidget)
# this makes me confused,I hope that when I clicked the date of the
# calendar ,then it will display the class of UI_tab.However it
# cant't work.
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
when I use the below code , it exactly does what I want it to do, but without clicking the calendar date.
Ui_Tab(self.centralWidget)
instead of
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
visit PyQt Window No Show
I just change the code below
self.cal.clicked.connect(lambda x :Ui_Tab(self.centralWidget))
to
self.cal.clicked.connect(Ui_Tab(self.centralWidget).show)
I created this widget using Qt creator and generated this code
from PySide import QtCore, QtGui
class Ui_DATA_Entries_Widget(QtGui.QWidget):
def setupUi(self, DATA_Entries_Widget):
DATA_Entries_Widget.setObjectName("DATA_Entries_Widget")
DATA_Entries_Widget.resize(676, 50)
DATA_Entries_Widget.setMinimumSize(QtCore.QSize(676, 50))
DATA_Entries_Widget.setMaximumSize(QtCore.QSize(676, 50))
self.horizontalLayout = QtGui.QHBoxLayout(DATA_Entries_Widget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtGui.QLabel(DATA_Entries_Widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.lineEdit = QtGui.QLineEdit(DATA_Entries_Widget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.label_2 = QtGui.QLabel(DATA_Entries_Widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.lineEdit_2 = QtGui.QLineEdit(DATA_Entries_Widget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout.addWidget(self.lineEdit_2)
self.label_3 = QtGui.QLabel(DATA_Entries_Widget)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.lineEdit_3 = QtGui.QLineEdit(DATA_Entries_Widget)
self.lineEdit_3.setObjectName("lineEdit_3")
self.horizontalLayout.addWidget(self.lineEdit_3)
self.label_5 = QtGui.QLabel(DATA_Entries_Widget)
self.label_5.setMinimumSize(QtCore.QSize(47, 32))
self.label_5.setObjectName("label_5")
self.horizontalLayout.addWidget(self.label_5)
self.lineEdit_4 = QtGui.QLineEdit(DATA_Entries_Widget)
self.lineEdit_4.setObjectName("lineEdit_4")
self.horizontalLayout.addWidget(self.lineEdit_4)
self.label_4 = QtGui.QLabel(DATA_Entries_Widget)
self.label_4.setObjectName("label_4")
self.horizontalLayout.addWidget(self.label_4)
self.lineEdit_5 = QtGui.QLineEdit(DATA_Entries_Widget)
self.lineEdit_5.setObjectName("lineEdit_5")
self.horizontalLayout.addWidget(self.lineEdit_5)
self.pushButton = QtGui.QPushButton(DATA_Entries_Widget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.retranslateUi(DATA_Entries_Widget)
QtCore.QMetaObject.connectSlotsByName(DATA_Entries_Widget)
def retranslateUi(self, DATA_Entries_Widget):
DATA_Entries_Widget.setWindowTitle(QtGui.QApplication.translate("DATA_Entries_Widget", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "A", None, QtGui.QApplication.UnicodeUTF8))
self.label_2.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "B", None, QtGui.QApplication.UnicodeUTF8))
self.label_3.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "C", None, QtGui.QApplication.UnicodeUTF8))
self.label_5.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "D", None, QtGui.QApplication.UnicodeUTF8))
self.label_4.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "E", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.translate("DATA_Entries_Widget", "Delete", None, QtGui.QApplication.UnicodeUTF8))
Now I need to add this widget dinamically, much like the the Test widget, that I got from another question:
from PySide import QtGui, QtCore
import sys
class Main(QtGui.QMainWindow):
def __init__(self, parent = None):
super(Main, self).__init__(parent)
# main button
self.addButton = QtGui.QPushButton('button to add other widgets')
self.addButton.clicked.connect(self.addWidget)
# scroll area widget contents - layout
self.scrollLayout = QtGui.QFormLayout()
# scroll area widget contents
self.scrollWidget = QtGui.QWidget()
self.scrollWidget.setLayout(self.scrollLayout)
# scroll area
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setWidget(self.scrollWidget)
# main layout
self.mainLayout = QtGui.QVBoxLayout()
# add all main to the main vLayout
self.mainLayout.addWidget(self.addButton)
self.mainLayout.addWidget(self.scrollArea)
# central widget
self.centralWidget = QtGui.QWidget()
self.centralWidget.setLayout(self.mainLayout)
# set central widget
self.setCentralWidget(self.centralWidget)
def addWidget(self):
self.scrollLayout.addRow(TestButton())
class TestButton(QtGui.QPushButton):
def __init__( self, parent=None):
super(TestButton, self).__init__(parent)
self.setText("I am in Test widget")
self.clicked.connect(self.deleteLater)
app = QtGui.QApplication(sys.argv)
myWidget = Main()
myWidget.show()
app.exec_()
How can I subtitute the TestButton in the second code with my custom widget easily?
Any help is much appreciated!
My objective is when open button is clicked, it will display all the files in the directory. When you click any of the directory it will display the content of the directory in an another frame. Now how will I connect each item in the list to a function which will grab the contents of the directory. Here is my code:
from PyQt4 import QtCore, QtGui
import os
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.frame = QtGui.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(20, 20, 361, 201))
self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame.setFrameShadow(QtGui.QFrame.Raised)
self.frame.setObjectName("frame")
self.listWidget = QtGui.QListWidget(self.frame)
self.listWidget.setGeometry(QtCore.QRect(0, 0, 91, 201))
self.listWidget.setObjectName("listWidget")
self.widget = QtGui.QWidget(self.frame)
self.widget.setGeometry(QtCore.QRect(110, 20, 120, 80))
self.widget.setObjectName("widget")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.add)
#QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("accepted()"), self.test)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def add(self):
item=[]
item = os.listdir('.')
for value in item:
self.listWidget.addItem(value)
def test(self):
print 'hello'
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Maybe a simpler way is to use these two signals:
itemClicked(QListWidgetItem * item)
or
itemDoubleClicked(QListWidgetItem * item)
from the QListWidget.
For example:
QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem)"), self.test)
which it should call the self.test method passing the current QListWidgetItem as parameter.