PyQT QListWidget drag and drop does not work - python

I am trying to implement in my project a QListWidget with the possibility of moving elements by drag and drop
I try to integrate it into the project in the simplest way without success, while outside I have no problem executing it.
EDIT:The problem seems to come from the realsense library, without its, DAD works
Here is its implementation:
priorityContainer.py:
class priorityContainer(QListWidget):
def __init__(self):
super().__init__()
self.setIconSize(QSize(124, 124))
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setDefaultDropAction(Qt.MoveAction)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.setAcceptDrops(True)
self.setDragEnabled(True)
for i in range(5):
QListWidgetItem( 'Item '+str(i), self)
main_interface.py:
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
import traceback, sys, os
import pyrealsense2 as rs
from ressource.interface import priorityContainer
class UI_main(QMainWindow):
def __init__(self):
super(UI_main, self).__init__()
self.setupUi()
self.show()
def setupUi(self):
self.centralwidget = QWidget(self)
self.mainVcontainer = QVBoxLayout(self.centralwidget)
self.listWidget = priorityContainer.priorityContainer()
self.mainVcontainer.addWidget(self.listWidget)
self.setCentralWidget(self.centralwidget)
def root_path(self):
return os.path.abspath(os.sep)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = UI_main()
sys.exit(app.exec_())

I have solved my problem by adding these lines before any other imports where I import my pyrealsense2 librairies:
import sys
sys.coinit_flags = 2
import pythoncom
Reference to the fix: https://github.com/IntelRealSense/librealsense/issues/6174

Related

How to make application window stay on top in pyqt5?

I'm trying to make a desktop application in pyqt5 that will stay on top of all windows. I've been looking around online and they all say that the solution is to set the window flags using the setWindowFlags(Qt.WindowStaysOnTopHint) method, but this isn't working for me. Is there some other way I can do this?
I'm on Windows 10 and using Python 3.6 + pyqt5 version 5.9.2. My code is as follows:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.initUI()
self.show()
def initUI(self):
self.alertWidget = AlertWidget()
self.setCentralWidget(self.alertWidget)
class AlertWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
grid.setAlignment(Qt.AlignTop)
self.alertTextBox = QTextEdit()
grid.addWidget(self.alertTextBox, 0, 0)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Main()
sys.exit(app.exec_())
Assuming the rest of your code is good, change the following line of code:
self.setWindowFlags(Qt.WindowStaysOnTopHint)
to the following line of code:
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint)
Link to an answer explaining why the code change above is required for the Qt.WindowStaysOnTop flag to work.

create main script to use PyQt4 windows with other objects

I am trying to learn how to use python and pyQt.
I have done a window with Qtcreator then I used pyuic4, I have also created a class called Ruban I would like to use it with my window interface. In my window I have a button called nouveauRuban. I would like to create an object from my class Ruban when this button is clicked.
I know my code is wrong, the problem may be at the initial part of mainTN, on the __init__ ?
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mainwindow import Ui_MainWindow
from Ruban import Ruban
class mainTM(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): #, parent=None ??
super (mainTM, self).__init__(self, parent) #(parent) ??
self.createWidgets()
self.nouveauRuban.connect(nouveauRuban, QtCore.SIGNAL(_fromUtf8("clicked()")), self.nvRuban)
def nvRuban(self):
self.ruban=Ruban()
self.ruban.info_ruban()
def createWidgets(self):
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
if __name__== "__main__":
app=QApplication(sys.argv)
myapp=mainTM()
myapp.show()
sys.exit(app.exec_())
Here is a re-write of your script which should fix all the problems:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from mainwindow import Ui_MainWindow
from Ruban import Ruban
class mainTM(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(mainTM, self).__init__(parent)
self.setupUi(self)
self.nouveauRuban.clicked.connect(self.nvRuban)
def nvRuban(self):
self.ruban = Ruban()
self.ruban.info_ruban()
if __name__== '__main__':
app = QApplication(sys.argv)
myapp = mainTM()
myapp.show()
sys.exit(app.exec_())
If you're connecting a signal to a slot, you need to define that slot using a decorator:
#QtCore.pyqtSlot()
def nvRuban(self):
self.ruban=Ruban()
self.ruban.info_ruban()
Then connect it:
self.nouveauRuban.clicked.connect(nvRuban)

PyQT: how to open new window

First of all, similar questions have been answered before, yet I need some help with this one.
I have a window which contains one button (Class First) and I want on pressed, a second blank window to be appeared (Class Second).
I fiddled with the code copied from this question: PyQT on click open new window, and I wrote this code:
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
import design1, design2
class Second(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
self.setupUi(self)
class First(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.exec_()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
but on_pressed, this error message appears:
AttributeError: 'Second' object has no attribute 'exec_'
(design1 and design2 have been derived from the Qt designer.)
Any thought would be appreciated.
Here I'm using the show method.
Here is a working example (derived from yours):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialog = Second(self)
def on_pushButton_clicked(self):
self.dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
If you need a new window every time you click the button, you can change the code that the dialog is created inside the on_pushButton_clicked method, like so:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.pushButton = QtGui.QPushButton("click me")
self.setCentralWidget(self.pushButton)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.dialogs = list()
def on_pushButton_clicked(self):
dialog = Second(self)
self.dialogs.append(dialog)
dialog.show()
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QLabel doesn't display pixmap

I'm trying to display a QPixmap in QLabel, but I get the label empty.
Here is my code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Test(QtGui.QWidget):
def __init__(self):
super(Test, self).__init__()
self.initUI()
def initUI(self):
pixmap = QtGui.QPixmap("test.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
self.move(300, 200)
self.setWindowTitle('Test')
self.show()
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
When I start the application, I only get an empty window :
I use a Linux distribution (Manjaro). I tested with Ubuntu, but I had the same problem.
I don't understand what is wrong, since I succeed to display other QPixmaps in QGraphicsScene and QIcon in QPushButton.
How can I display a QPixmap in my label ?
EDIT:
Here is an other version with a layout, but which don't solve the problem:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Test(QtGui.QWidget):
def __init__(self):
super(Test, self).__init__()
self.initUI()
def initUI(self):
layout = QtGui.QVBoxLayout()
pixmap = QtGui.QPixmap("test.png")
lbl = QtGui.QLabel(self)
lbl.setPixmap(pixmap)
layout.addWidget(lbl)
self.setLayout(layout)
self.move(300, 200)
self.setWindowTitle('test')
self.show()
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
The trouble not is the path
The trouble is when execute the program from VSC
If you execute the program in CMD or TERMINAL the program RUN and Show the Image !
Tested !

pyside QGraphicsScene: Why it is not working?

I am new to Qt and PySyde. I trying to create a small app to visualize sime line drawings.
In order to do that I try to use QGraphicsView an QGraphicsScene. I made a test to learn how it is working but it isn't. I googled a lot around, I do not understan why it isn't working.
Can somebody explain me the reason and bring me the light?
My code (just want to put a line and a sample text on the scene):
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox=QtGui.QHBoxLayout()
leftpanel=QtGui.QFrame()
leftpanel.setGeometry(0,0,300,400)
scene=QtGui.QGraphicsScene()
scene.addText("Hello, world!")
view=QtGui.QGraphicsView(scene,leftpanel)
view.setSceneRect(0,0,300,400)
pen=QtGui.QPen(QtCore.Qt.black,2)
scene.addLine(0,0,200,200,pen)
hbox.addWidget(leftpanel)
rightpanel=QtGui.QFrame()
hbox.addWidget(rightpanel)
szoveg=QtGui.QLabel(rightpanel)
szoveg.setText(u"Hello World!")
self.setLayout(hbox)
self.resize(500,500)
self.setWindowTitle('blabla')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You need to save reference to scene somewhere, e.g. in Example instance:
def initUI(self):
# ...
scene = QtGui.QGraphicsScene()
self.scene = scene # save reference to scene, or it will be destroyed
scene.addText("Hello, world!")
# ...
In another function, you'll be able to add more items to the scene:
def anotherFunction(self):
self.scene.addText("Another Hello, world!")

Categories

Resources