Unable to add a QTextEdit inside QTreeWidget - python

I'm trying to add an option for a QTreeWidget to have multi line editing, which I would assume will require a QTextEdit. The problem is that the examples I've found online just do not work.
The answers I've found have all pointed to using tree.setItemWidget(item, column, widget), but If I add that line, the window just doesn't appear at all. What am I doing wrong in this case?
Here is my example code that has the issue:
import sys
from Qt import QtWidgets, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent, **kwargs)
#Add tree widget to window
tree = QtWidgets.QTreeWidget()
tree.setHeaderItem(QtWidgets.QTreeWidgetItem(['col1', 'col2']))
self.setCentralWidget(tree)
#Create items
topLevelButton = QtWidgets.QPushButton('button')
topLevelItem = QtWidgets.QTreeWidgetItem(['test button', 'line edit'])
topLevelItem.setFlags(topLevelItem.flags() | QtCore.Qt.ItemIsEditable)
#Add items to tree widget
tree.addTopLevelItem(topLevelItem)
tree.setItemWidget(topLevelItem, 0, topLevelButton) #the window will not load if this line is not commented out
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.setActiveWindow(window)
window.show()
sys.exit(app.exec_())
I've tried it in PySide (2.7) and PySide2 (3.7).
Edit: For Python 3 at least, it seemed to be an issue with PySide2, where forcing PyQt5 somehow fixed whatever it was. I'm still unable to launch with Python 2 as I can't really install PyQt4.
Edit 2: It actually causes a crash if you use it in a program such as Nuke that uses PySide, I may need to ask a more specific question if I can't figure it out from this one.

Sorry, PyQt5 is working.
import sys
#from Qt import QtWidgets, QtCore
from PyQt5 import QtWidgets, QtCore # <---
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent, **kwargs)
# Add tree widget to window
tree = QtWidgets.QTreeWidget()
tree.setHeaderItem(QtWidgets.QTreeWidgetItem(['col1', 'col2']))
self.setCentralWidget(tree)
# Create items
topLevelButton = QtWidgets.QPushButton('button')
topLevelItem = QtWidgets.QTreeWidgetItem(['test button', 'line edit'])
topLevelItem.setFlags(topLevelItem.flags() | QtCore.Qt.ItemIsEditable)
# Add items to tree widget
tree.addTopLevelItem(topLevelItem)
tree.setItemWidget(topLevelItem, 0, topLevelButton) # ??? the window will not load if this line is not commented out
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.setActiveWindow(window) # ???
window.show()
sys.exit(app.exec_())

Related

Python PyQt Change layout of combobox

Is there a way (or maybe a different widget i can use (i couldn't find any) so that i can make my combobox look something like the combobox in the picture ?
The picture shows what i want put with font. I would like the options to be my list instedt of the font.
I would like to see all the option (if there are to many to be able to scroll) and select one.
Here is my code so far:
from PyQt5 import QtCore, QtWidgets
import sys
from PyQt5 import QtGui, QtWidgets, QtPrintSupport
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.bb = QtWidgets.QComboBox(self)
self.bb.setGeometry(QtCore.QRect(212, 50, 400, 25))
self.bb.setEditable(True)
bpas = ['a','b','c']
self.bb.addItem("")
self.bb.setItemText(0, "")
for bpa in bpas:
self.bb.addItem(bpa)
self.bb.move(50, 200)
self.bb.activated[str].connect(self.style_choice)
def font_choice(self):
font, valid = QtWidgets.QFontDialog.getFont()
if valid:
self.styleChoice.setFont(font)
def style_choice(self, text):
self.styleChoice.setText(text)
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create(text))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
gui.show()
app.exec_()
Edit:
I would like that when the whindow opens that all my option are always beeing shown. So that i do not have to press the dropdown arror. In different words: If the window is beeing opened there is a list of bpas, i want to be able to select from the list one of the bpa/option and send a singal that i choose this bpa.
To explain myself a little more (this is not been shown anywhere in the code): bpas = ['a','b','c'] are projects, and I want the user to seleced one of them and after selecting it the programm will load its conneced database. With dropdown it works, but i dont like the way dorpdown looks like with a lot of options :)
You can use a QListWidget with a QLineEdit:
from PyQt5 import QtCore, QtWidgets
import string
class Thired(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Thired, self).__init__(parent)
self.line_edit = QtWidgets.QLineEdit()
self.list_widget = QtWidgets.QListWidget()
options = list(string.ascii_letters)
self.list_widget.addItems(options)
self.list_widget.itemClicked.connect(self.on_itemClicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.line_edit)
lay.addWidget(self.list_widget)
self.resize(640, 480)
#QtCore.pyqtSlot(QtWidgets.QListWidgetItem)
def on_itemClicked(self, item):
self.line_edit.setText(item.text())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
gui = Thired()
gui.show()
sys.exit(app.exec_())

How can i create Multi-page applications in PyQt4?

I had a little research regarding this question, but i couldn't find any simple answer or tutorial to do this.
I'm trying to find a widget, that will make transition between pages using QPushButton. I have heard of QStackWidget, But i'm not exactly sure how to use it, I have found many tutorials, but all of them were hard to understand.
So for example, i have a window class:
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
How can i add QStackWidget to this class? (Without any layouts)
If i do so, how can i switch to specific page using QPushButton? (With adding multiple widgets in one index)
extra: is it possible to add some kind of effect on transition? like fading, etc.
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.stacked_widget = QtGui.QStackedWidget()
self.button = QtGui.QPushButton("Next")
self.button.clicked.connect(self.__next_page)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.stacked_widget)
layout.addWidget(self.button)
widget = QtGui.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.stacked_widget.addWidget(QtGui.QLabel("Page 1"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 2"))
self.stacked_widget.addWidget(QtGui.QLabel("Page 3"))
def __next_page(self):
idx = self.stacked_widget.currentIndex()
if idx < self.stacked_widget.count() - 1:
self.stacked_widget.setCurrentIndex(idx + 1)
else:
self.stacked_widget.setCurrentIndex(0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

Access QT Designer Objects Programmatically

Disclaimer: New to both python and qt designer
QT Designer 4.8.7
Python 3.4
PyCharm 5.0.3
Question - How do I add controls to the main form or a scroll area widget on the main form (created in QT Designer) programmatically?
I have created a MainWindow in qt designer and added my widgets. The following is the entire test program in PyCharm:
import sys
from PyQt4 import QtGui, QtCore, uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
qtCreatorFile = "programLauncher.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
# self.scrollArea_programs.addWidget()
grid = QtGui.QGridLayout()
# self.scrollArea_programs.addWidget(self.pushButton)
grid.addWidget(self.dateLabel,0,0)
grid.addWidget(self.pushButton,0,1)
self.setLayout(grid)
self.pushButton_exit.clicked.connect(self.closeEvent)
def closeEvent(self):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
As you can see I tried to add controls to a grid but nothing shows up when the program runs - I have also tried to add a control to the scroll area. Can someone help me to just add 1 control to the scroll area at run time - so then I can know the proper way to do it or "a" proper way to do this.
Thanks in advance
Without having access to your programLauncher.ui and making minimal changes to your posted code, you can add your UI elements to the window like so:
from PyQt4 import QtGui
import sys
class MyApp(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
# Cannot resize or maximize
self.setFixedSize(1045, 770)
widget = QtGui.QWidget(self)
self.setCentralWidget(widget)
# Add button test
self.dateLabel = QtGui.QLabel("Test")
self.pushButton = QtGui.QPushButton('Test button')
grid = QtGui.QGridLayout()
grid.addWidget(self.dateLabel, 0, 0)
grid.addWidget(self.pushButton, 0, 1)
widget.setLayout(grid)
self.pushButton.clicked.connect(self.closeEvent)
def closeEvent(self, event):
QtGui.QApplication.quit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
This will get the controls on the screen, although the layout leaves a lot to be desired. You may have to make modifications to this based on what's in your .ui file. One thing that you'll want to note in this example is that the QMainWindow needs a central widget (widget in the example above). You then set the layout on that widget.
You can use the designer to create your .ui file
The you can load it in your .py using something like:
from PyQt4 import QtCore, QtGui, uic
class my_win(QtGui.QMainWindow):
def __init__(self):
self.ui = uic.loadUi('my_ui.ui',self)
then you can access all your widgets with something like
self.ui.actionQuit.triggered.connect(QtGui.qApp.quit)
or
self.ui.my_button.triggered.connect(self.do_someting)
Thanks to JCVanHamme (the programLauncher.ui hint) and also outside help I now learned most of what I need to know to access MainWindow at run time. So for anyone interested in this beginner tip:
Take a blank form in QT Designer
Add a control
Run pyuic4 batch file
Take a look at the generated .py file to learn EVERYTHING about how to add controls.
Don't let the power go to your head - cheers

Pyqt4, can not open new window in python [duplicate]

Okay... This has been bugging me for hours. I have a qtmainwindow with a menubar. I've managed to connect an action in tje menubar to an independent Qwidget. But as soon as the Qwidget appears it disappears. I'm using the latest version of pyqt.
Here's the code:
Import sys
from PyQt4 import QtGui, QtCore
Class Main(QtGui.QtMainWindow) :
def __init__(self) :
QtGui.QtMainWindow.__init__(self)
self.setGeometry(300,300,240,320)
self.show()
menubar = self. menuBar()
filemenu = menubar. addMenu('&File')
new = QtGui.QAction(QtGui.QIcon('new.png'), 'New', self)
new.triggered.connect(self.pop)
filemenu.addAction(new)
def pop(self) :
pop = Pop()
class Pop(QtGui.QWidget) :
def __init__(self) :
QtGui.QWidget.__init__(self)
self.setGeometry(300,300,240,320>
self.setWindowTitle('Pop up')
self.show()
Update the pop(self) method as:
def pop(self):
self.window = Pop()
you need to store object of newly created window in a member variable, other wise as soon as the method finishes with the execution, local variables will be destroyed by the Python Garbage Collector.
if you implement this code, you will see the window gets created and disappears immediately.
import sys
from PyQt5 import QtGui, QtWidgets,QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(50,50,500,500)
window.setWindowTitle("GUI window")
window.show()
To solve that problem write "sys.exit(app.exec_())" after window.show() and the window will stay on the screen.

Howto embed a video via mpylayer into a QWidget frame?

Is it possible in PyQt4 to embed a video via mpylayer into a QWidget (or into a subclass of it). If so, could you provide a minimal working example.
For a complete example of a Qt Widget that embeds MPlayer, try qmpwidget.
But here's a minimal PyQt demo to get you started:
import mpylayer
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)
self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.container)
layout.addWidget(self.button)
self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])
def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
(NB: this demo has only been tested on Linux)
You have to get the handle (id) of the widget → http://qt-project.org/doc/qt-4.8/qwidget.html#winId
And pass it to the -wid option of the MPlayer.
I can't provide you an example with Qt, simply because I don't know Qt, but I already wrote an MplayerCtrl for wxPython: https://bitbucket.org/dav1d/mplayerctrl
Relevant Code: https://bitbucket.org/dav1d/mplayerctrl/src/c680a1d99ad2/MplayerCtrl.py#cl-873

Categories

Resources