PyQt4 : Connect splitter - python

I want to connect splitters to make a "cross" splitters between 4 widgets. I tried to do it using signal and slot between two splitters, and even if no errors occured, the behavior is not the one I hope (in fact nothing happens).
import sys
from PyQt4 import QtGui, QtCore
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.initUI()
self.window.setFocus()
self.setCentralWidget(self.window)
self.showMaximized()
def initUI(self) :
self.window = QtGui.QWidget()
self.editor1 = QtGui.QTextEdit()
self.editor2 = QtGui.QTextEdit()
self.editor3 = QtGui.QTextEdit()
self.editor4 = QtGui.QTextEdit()
self.split1 = QtGui.QSplitter()
self.split2 = QtGui.QSplitter()
self.split3 = QtGui.QSplitter()
self.split2.setOrientation(QtCore.Qt.Vertical)
self.split3.setOrientation(QtCore.Qt.Vertical)
self.split2.addWidget(self.editor1)
self.split2.addWidget(self.editor2)
self.split3.addWidget(self.editor3)
self.split3.addWidget(self.editor4)
self.connect(self.split2, QtCore.SIGNAL("splitterMoved"), self.split3.moveSplitter)
self.connect(self.split3, QtCore.SIGNAL("splitterMoved"), self.split2.moveSplitter)
self.split1.addWidget(self.split2)
self.split1.addWidget(self.split3)
self.layout = QtGui.QHBoxLayout()
self.layout.addWidget(self.split1)
self.window.setLayout(self.layout)
def main() :
qApp = QtGui.QApplication(sys.argv)
qApp.setStyle('cleanlooks')
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())
if __name__ == '__main__':
main()
Any ideas ?

Ok finally I found how to do that using python :
import sys
from PyQt4 import QtGui, QtCore
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Cluster View")
self.initUI()
self.window.setFocus()
self.setCentralWidget(self.window)
self.showMaximized()
def splitterMoved(self, sender) :
print ("ok", sender)
receiver = self.split2 if sender is self.split3 else self.split3
receiver.blockSignals(True)
receiver.setSizes(sender.sizes())
receiver.blockSignals(False)
def initUI(self) :
self.window = QtGui.QWidget()
self.editor1 = QtGui.QTextEdit()
self.editor2 = QtGui.QTextEdit()
self.editor3 = QtGui.QTextEdit()
self.editor4 = QtGui.QTextEdit()
self.split1 = QtGui.QSplitter()
self.split2 = QtGui.QSplitter()
self.split3 = QtGui.QSplitter()
self.split2.setOrientation(QtCore.Qt.Vertical)
self.split3.setOrientation(QtCore.Qt.Vertical)
self.split2.addWidget(self.editor1)
self.split2.addWidget(self.editor2)
self.split3.addWidget(self.editor3)
self.split3.addWidget(self.editor4)
self.connect(self.split2, QtCore.SIGNAL("splitterMoved(int, int)"), lambda x : self.splitterMoved(self.split2))
self.connect(self.split3, QtCore.SIGNAL("splitterMoved(int, int)"), lambda x : self.splitterMoved(self.split3))
self.split1.addWidget(self.split2)
self.split1.addWidget(self.split3)
self.layout = QtGui.QHBoxLayout()
self.layout.addWidget(self.split1)
self.window.setLayout(self.layout)
def main() :
qApp = QtGui.QApplication(sys.argv)
qApp.setStyle('cleanlooks')
aw = ApplicationWindow()
aw.show()
sys.exit(qApp.exec_())
if __name__ == '__main__':
main()
Maybe there are better way to do that, but at least it works now.

Related

Why other window(GUI) is not opening while other window(GUI) is running in Python?

I have a scenario where one window is running in python Pyqt5. I want that when certain event happens another window also opens up.
I have written a code which I suppose should run fine but when event occurs to open other GUI, I gets an error.
My code:
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel("URL:")
url = QLineEdit()
l3 = QLabel("Wait (sec):")
wait = QLineEdit()
l2 = QLabel("Iteration:")
l2.move(20,100)
global add1
count = QLineEdit()
fbox = QFormLayout()
fbox.addRow(l1, url)
fbox.addRow(l3, wait)
vbox = QVBoxLayout()
vbox.addWidget(count)
fbox.addRow(l2, vbox)
startButton=QPushButton("Start")
fbox.addRow(startButton)
startButton.clicked.connect(self.requests)
win.setLayout(fbox)
win.setWindowTitle("--")
win.resize(300,200)
win.show()
sys.exit(app.exec_())
def requests(self):
for x in range(0,int(count.text())):
//certain event happens here, which will cause other window to get open
self.dialog = PopUp(self)
self.dialog.show()
def stop(self):
sys.exit()
class PopUp(QMainWindow):
def __init__(self, parent=None):
super(PopUp, self).__init__(parent)
app = QApplication(sys.argv)
win = QWidget()
l1 = QLabel("URL:")
nextUrl = QLineEdit()
fbox = QFormLayout()
fbox.addRow(l1, url)
startButton = QPushButton("Go")
fbox.addRow(startButton)
startButton.clicked.connect(self.requests)
win.setLayout(fbox)
win.setWindowTitle("--")
win.resize(300, 200)
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
I see that code is right but I am getting an unknown error:
QCoreApplication::exec: The event loop is already running
I have searched on google and here in stack overflow but didn't get anything worthy. Anyone knows that why this error comes and why is it coming in my code??
Each PyQt5 application must create one application object app = QApplication(sys.argv). Remove from the class MainWindow and the class PopUp - app = QApplication (sys.argv) .
sys.exit(app.exec_()) - the mainloop of the application, he must also be alone. Remove from the class MainWindow and the class PopUp - sys.exit(app.exec_())
I improved the readability of your example a bit.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class PopUp(QMainWindow):
def __init__(self, x, url, parent=None):
super(PopUp, self).__init__(parent)
self.url = url.text()
self.x = x + 1
self.setWindowTitle("-- PopUp {} --".format(self.x))
self.setGeometry(self.x*100, self.x*100, 300, 200)
win = QWidget()
self.setCentralWidget(win)
nextUrl = QLineEdit(self.url)
self.startButton = QPushButton("Go {}".format(self.x))
fbox = QFormLayout(win)
fbox.addRow(QLabel("URL:"), nextUrl)
fbox.addRow(self.startButton)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.dialogs = []
self.flag = False
win = QWidget()
self.setCentralWidget(win)
self.url = QLineEdit() # + self
wait = QLineEdit()
self.count = QLineEdit() # + self
startButton = QPushButton("Start")
startButton.clicked.connect(self.requests)
fbox = QFormLayout(win)
fbox.addRow(QLabel("URL:"), self.url)
fbox.addRow(QLabel("Wait (sec):"), wait)
fbox.addRow(QLabel("Iteration:"), self.count)
fbox.addRow(startButton)
def requests(self):
if self.dialogs and self.flag:
_ = [ i.hide() for i in self.dialogs]
self.dialogs = []
for x in range(0, int(self.count.text())):
# certain event happens here, which will cause other window to get open
dialog = PopUp(x, self.url, self)
dialog.startButton.clicked.connect(self.requests2)
dialog.show()
self.dialogs.append(dialog)
self.flag = True
def stop(self): # ?
sys.exit()
def requests2(self):
print("def requests2(self): clicked Button {}".format(self.sender().text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWindow()
main.setWindowTitle("-- Title --")
main.resize(300,200)
main.show()
sys.exit(app.exec_())

PyQt Change Direction of QWidget from right to left

I would like to change a direction of widget From Right to left by using SetLyouatDirection but does not work
This my result :
this my code :
for item in listConcrdance:
c+=1
widgitItem = QtGui.QListWidgetItem()
widget = QtGui.QWidget()
widgetText=QtGui.QLabel(str(c)+". "+item[1]+" ("+self.process.convertNumberToNameOFSorat(item[0][1])+":"+item[0][2]+")")
widgetText.setLayoutDirection(QtCore.Qt.RightToLeft)
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widgetLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
widget.setLayout(widgetLayout)
self.listWidgetQuran.addItem(widgitItem)
widgitItem.setSizeHint(widget.sizeHint())
self.listWidgetQuran.setItemWidget(widgitItem, widget)
Use [your_label].setAlignment(QtCore.Qt.AlignRight):
from PyQt4 import QtCore
from PyQt4 import QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.listWidget = QtGui.QListWidget(self)
self.verticalLayout.addWidget(self.listWidget)
for item in range(10):
widgitItem = QtGui.QListWidgetItem(self.listWidget)
widget = QtGui.QWidget()
widgetText = QtGui.QLabel(str(item))
if item % 2 == 0:
widgetText.setAlignment(QtCore.Qt.AlignRight)
else:
widgetText.setAlignment(QtCore.Qt.AlignLeft)
widgetLayout = QtGui.QHBoxLayout()
widgetLayout.addWidget(widgetText)
widget.setLayout(widgetLayout)
widgitItem.setSizeHint(widget.sizeHint())
self.listWidget.setItemWidget(widgitItem, widget)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Output:

TypeError: check() missing 1 required positional argument: 'theText'

I'm making a small PyQt4 application and so far I have this:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check(self.text))
self.show()
def check(self, state, theText):
if state == QtCore.Qt.Checked:
theText.setStyleSheet("color: pink")
else:
theText.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
The problem is, my .connect line doesn't seem to want to recognize the parameter I pass into it and I'm not sure why. I can't reference the text directly in the function as I would like to pass many QLabel arguments to it hence I made a theText parameter. I'd appreciate any help.
Version that works without parameters:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
self.textCheckBox.stateChanged.connect(self.check)
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.text)
self.hbox.addWidget(self.textCheckBox)
self.setLayout(self.hbox)
self.show()
def check(self, state):
if state == QtCore.Qt.Checked:
self.text.setStyleSheet("color: pink")
else:
self.text.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUi()
def initUi(self):
self.setFixedSize(500, 300)
self.text = QtGui.QLabel('Text to be changed')
self.text2 = QtGui.QLabel('Also to be changed')
self.textCheckBox = QtGui.QCheckBox()
test = self.textCheckBox.checkState()
self.textCheckBox.stateChanged.connect(lambda: self.check(self.textCheckBox.checkState(), self.text))
self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.text)
self.hbox.addWidget(self.textCheckBox)
self.setLayout(self.hbox)
self.show()
def check(self, state, theText):
if state == QtCore.Qt.Checked:
self.setWindowTitle("test")
else:
theText.setStyleSheet("color: black")
def main():
q = QtGui.QApplication(sys.argv)
w = Window()
sys.exit(q.exec())
if __name__=='__main__':
main()
Seem to have fixed it. Not too sure why it works though.

force python pyside splitter to start window center

How can I force the splitter to be positioned in the center of the window at the start? As you can see in the code below it favors the right side because of the button being small. however I would like to have the splitter always appear in the middle of the window as shown in image two.
Current
Goal
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(550, 400)
self.setWindowTitle("Cameras")
# widgets
self.ListA = QtGui.QTreeWidget()
self.ListB = QtGui.QTreeWidget()
self.Button = QtGui.QPushButton()
# layout Splitter
self.mainLayout = QtGui.QHBoxLayout(self)
self.mainLayout.setContentsMargins(5,5,5,5)
self.leftPanel = QtGui.QFrame(self)
# self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
self.leftPanelLayout.setContentsMargins(0,0,0,0)
self.leftPanelLayout.addWidget(self.ListA)
self.rightPanel = QtGui.QFrame(self)
# self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
self.rightPanelLayout.setContentsMargins(0,0,0,0)
self.rightPanelLayout.addWidget(self.Button)
self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.leftPanel)
self.splitter.addWidget(self.rightPanel)
self.mainLayout.addWidget(self.splitter)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('QtGui.QSplitter')
self.show()
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Bam! got it.
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(1000, 1000)
self.setWindowTitle("Cameras")
# widgets
self.ListA = QtGui.QTreeWidget()
self.ListB = QtGui.QTreeWidget()
self.Button = QtGui.QPushButton()
# layout Splitter
# QHBoxLayout
self.mainLayout = QtGui.QGridLayout(self)
self.mainLayout.setContentsMargins(5,5,5,5)
self.leftPanel = QtGui.QFrame(self)
# self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
self.leftPanelLayout.setContentsMargins(0,0,0,0)
self.leftPanelLayout.addWidget(self.ListA)
self.rightPanel = QtGui.QFrame(self)
# self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
self.rightPanelLayout.setContentsMargins(0,0,0,0)
self.rightPanelLayout.addWidget(self.Button)
self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.leftPanel)
self.splitter.addWidget(self.rightPanel)
self.splitter.setCollapsible(0,False)
self.splitter.setCollapsible(1,False)
self.mainLayout.addWidget(self.splitter,0,0)
self.setWindowTitle('QtGui.QSplitter')
self.show()
self.set_panel_sizes(self.splitter)
def onChanged(self, text):
self.lbl.setText(text)
self.lbl.adjustSize()
def set_panel_sizes(self, ctrl):
width = ctrl.frameSize().width() / 2.0
ctrl.setSizes( [width,width] )
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Python: How to to query a current Tab from QTabWidget

A simple dialog with three tabs: 'Tab_01', 'Tab_02' and 'Tab_03'.
Pressing 'What Tab?' button should print what tab is currently active.
How to achieve it? (example code posted below):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
mainWidget=QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtGui.QTabWidget()
mainLayout.addWidget(self.tabWidget)
self.tabWidget.connect(self.tabWidget, QtCore.SIGNAL("currentChanged(int)"), self.tabSelected)
myBoxLayout = QtGui.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtGui.QWidget(),'Tab_01')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_02')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_03')
ButtonBox = QtGui.QGroupBox()
ButtonsLayout = QtGui.QHBoxLayout()
ButtonBox.setLayout(ButtonsLayout)
Button_01 = QtGui.QPushButton("What Tab?")
ButtonsLayout.addWidget(Button_01)
Button_01.clicked.connect(self.whatTab)
mainLayout.addWidget(ButtonBox)
def tabSelected(self, arg=None):
print '\n\t tabSelected():', arg
def whatTab(self):
print '\n\t current Tab:', '?'
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
As you can see here QTabWidget has methods currentIndex and currentWidget.
Here is the solution (thanks Hyperboreus !):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
mainWidget=QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
self.tabWidget = QtGui.QTabWidget()
mainLayout.addWidget(self.tabWidget)
self.tabWidget.connect(self.tabWidget, QtCore.SIGNAL("currentChanged(int)"), self.tabSelected)
myBoxLayout = QtGui.QVBoxLayout()
self.tabWidget.setLayout(myBoxLayout)
self.tabWidget.addTab(QtGui.QWidget(),'Tab_01')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_02')
self.tabWidget.addTab(QtGui.QWidget(),'Tab_03')
ButtonBox = QtGui.QGroupBox()
ButtonsLayout = QtGui.QHBoxLayout()
ButtonBox.setLayout(ButtonsLayout)
Button_01 = QtGui.QPushButton("What Tab?")
ButtonsLayout.addWidget(Button_01)
Button_01.clicked.connect(self.whatTab)
mainLayout.addWidget(ButtonBox)
def tabSelected(self, arg=None):
print '\n\t tabSelected() current Tab index =', arg
def whatTab(self):
currentIndex=self.tabWidget.currentIndex()
currentWidget=self.tabWidget.currentWidget()
print '\n\t Query: current Tab index =', currentIndex
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

Categories

Resources