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_())
Related
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.
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:
I create 2 windows testWidget and win2. First in testWidget, I click "Input maesure data " in Start menu, then I want to input number in LineEdit of L1(m) in win2, press "Analyze" and shows it in the testWidget. I did it, but it will pop up testWidget again. How to solve it?
Thanks!
import sys
from PyQt4 import QtGui,QtCore
class testWidget(QtGui.QMainWindow):
def __init__(self):
super(testWidget, self).__init__()
self.setGeometry(25,150,1200,700)
self.setWindowTitle('test')
extractAction_1 = QtGui.QAction('&Input maesure data',self)
extractAction_1.triggered.connect( self.newWindow)
self.linetest = QtGui.QLineEdit()
self.linetest.setText("0.0")
layoutV = QtGui.QVBoxLayout()
layoutV.addWidget(self.linetest)
widget = QtGui.QWidget()
widget.setLayout(layoutV)
self.setCentralWidget(widget)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&Start')
fileMenu.addAction(extractAction_1)
def newWindow(self):
self.myOtherWindow = win2()
self.myOtherWindow.show()
def showtex(self,text_LT):
self.linetest.clear()
self.linetest.setText(text_LT)
class win2(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Set parameters")
self.setGeometry(150,300,300,200)
b2 = QtGui.QPushButton("Analyze")
b2.clicked.connect(self.getre)
layoutV = QtGui.QVBoxLayout()
layoutH = QtGui.QHBoxLayout()
lab1 = QtGui.QLabel("L1(m):")
self.line1 = QtGui.QLineEdit()
self.line1.setText("50")
lab2 = QtGui.QLabel("L2(m):")
self.line2 = QtGui.QLineEdit()
self.line2.setText("0.0")
self.line3test = QtGui.QLineEdit()
layoutH.addWidget(lab1)
layoutH.addWidget(self.line1)
layoutH.addWidget(lab2)
layoutH.addWidget(self.line2)
layoutV.addLayout(layoutH)
layoutV.addWidget(b2)
layoutV.addWidget(self.line3test)
self.widget = QtGui.QWidget()
self.widget.setLayout(layoutH)
self.widget.setLayout(layoutV)
self.setCentralWidget(self.widget)
self.winma=testWidget()
def getre(self):
text_LT = self.line1.text()
self.winma.showtex(text_LT)
self.winma.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = testWidget()
GUI.show()
app.exec_()
You need to set a global variable for acessing.
GUI = None
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
GUI = testWidget()
GUI.show()
app.exec_()
And, let the subwindow pass value to the GUI.
def getre(self):
text_LT = self.line1.text()
GUI.linetest.setText(text_LT)
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()
There is a single QListWidget with ExtendedSelection enabled (multiple items can be selected at the same time). Clicking button queries listWidget.currentItem() for get a current item from listWidget. Question: how to get all the items that are currently selected in listWidget?
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.listWidget = QtGui.QListWidget()
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
for i in range(3):
item=QtGui.QListWidgetItem()
name='A'+'%04d'%i
item.setText(name)
self.listWidget.addItem(item)
myBoxLayout.addWidget(self.listWidget)
Button_01 = QtGui.QPushButton("Print Current Items")
Button_01.clicked.connect(self.printCurrentItems)
myBoxLayout.addWidget(Button_01)
def printCurrentItems(self):
print "Current Items are : ", self.listWidget.currentItem()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(720,480)
sys.exit(app.exec_())
Here is the working code (thanks to user3254944!):
from PyQt4 import QtGui, QtCore
import sys, os
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.listWidget = QtGui.QListWidget()
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
for i in range(3):
item=QtGui.QListWidgetItem()
name='A'+'%04d'%i
item.setText(name)
self.listWidget.addItem(item)
myBoxLayout.addWidget(self.listWidget)
Button_01 = QtGui.QPushButton("Print Current Items")
Button_01.clicked.connect(self.printCurrentItems)
myBoxLayout.addWidget(Button_01)
def printCurrentItems(self):
print "Current Items are : ", self.listWidget.selectedItems()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(720,480)
sys.exit(app.exec_())
.selectedItems()
C++ for QTableWidget but the same for QListWidget
QList<QTableWidgetItem*> selectedItems = matrixTable.selectedItems();