I try to run this code, but it always get this AttributeError, I have searched for many website but there wasn't any answer.
QtWidgets.QDesktopWidget().availableGeometry().center()
AttributeError: module 'PyQt6.QtWidgets' has no attribute 'QDesktopWidget'
My Code:
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def center(self):
qr = Form.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
I'm using PyQt6 Version 6.1.0, Python 3.9.5
From the docs:
QDesktopWidget and QApplication::desktop() QDesktopWidget was already
deprecated in Qt 5, and has been removed in Qt 6, together with
QApplication::desktop().
QScreen provides equivalent functionality to query for information
about available screens, screen that form a virtual desktop, and
screen geometries.
Use QWidget::setScreen() to create a QWidget on a specific display;
note that this does not move a widget to a screen in a virtual desktop
setup.
Then use:
cp = QtGui.QGuiApplication.primaryScreen().availableGeometry().center()
You can try this
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def center(self):
qr=self.frameGeometry()
cp=QtGui.QGuiApplication.primaryScreen().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
Related
I refer to the PySide MDI example in PySide project repository at https://github.com/pyside/Examples/blob/master/examples/mainwindows/mdi/mdi.py. I tried to adopt the same to PySide6 after changing the import reference to from PySide to PySide6. It is working fine in current form.
After making sure that the example works with PySide6, I made following additions:
Implemented a new Widget in Qt Designer (using exe installed in project VE using pip install pyside6) with just one QTextEdit widget on this form. The python code generated by designer is as below:
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'FormWidget01uyjQCJ.ui'
##
## Created by: Qt User Interface Compiler version 6.4.0
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QSizePolicy, QTextEdit, QWidget)
class Ui_FormWidget01(object):
def setupUi(self, FormWidget01):
if not FormWidget01.objectName():
FormWidget01.setObjectName(u"FormWidget01")
FormWidget01.resize(700, 500)
self.textEdit = QTextEdit(FormWidget01)
self.textEdit.setObjectName(u"textEdit")
self.textEdit.setGeometry(QRect(10, 10, 681, 481))
self.retranslateUi(FormWidget01)
QMetaObject.connectSlotsByName(FormWidget01)
# setupUi
def retranslateUi(self, FormWidget01):
FormWidget01.setWindowTitle(QCoreApplication.translate("FormWidget01", u"Form Widget", None))
# retranslateUi
In the mdi.py application, I added the following class for newly generated child on pattern of class MdiChild(QTestEdit) in the above example:
class FormWidget01(Ui_FormWidget01):
def __init__(self):
super(FormWidget01, self).__init__()
self.setAttribute(Qt.WA_DeleteOnClose)
self.isUntitled = True
Finally, I updated class MainWindow(QMainWindow) as below:
Added new function on pattern of newfile(self):
def newFw01(self):
_fw01 = self.createFormWidget01()
_fw01.show()
Added new function on pattern of createMdiChild(self):
def createFormWidget01(self):
fw01 = FormWidget01()
fw01.resize(400, 300)
self.mdiArea.addSubWindow(fw01)
return fw01
And updated createActions(self):
self.newFw01Act = QAction(QIcon(':/images/new.png'), "&New FW01", self,
shortcut=QKeySequence.New, statusTip="Create a new FW",
triggered=self.newFw01)
When I run the updated program and click action for new widget, I get the following error:
File "S:\***\newmdi\mdiapp.py", line 61, in __init__
self.setAttribute(Qt.WA_DeleteOnClose)
AttributeError: 'FormWidget01' object has no attribute 'setAttribute'
At this point, I realized that C++ code generated by the Designer initiate form is like this:
class Ui_FormWidget01
{
public:
QTextEdit *textEdit;
void setupUi(QWidget *FormWidget01)
{
if (FormWidget01->objectName().isEmpty())
FormWidget01->setObjectName("FormWidget01");
FormWidget01->resize(700, 500);
textEdit = new QTextEdit(FormWidget01);
textEdit->setObjectName("textEdit");
...
If I change the python UI class from class Ui_FormWidget01(object): to class Ui_FormWidget01(QWidget):, replacing object with QWidget, the action works but produces a minimized and blank instance of Ui_FormWidget01. There is no QTextEdit visible on it.
I'm not pro at coding and not sure what is going wrong.
In partcular, I need to understand if:
It is possible to create a custom Widget in Qt Designer and use it as MdiChild in Qt MDI application Framework?
If a child widget initialized with QTextEdit works as MDI child but not a child with initialized with object, what could be restrictions on type of an MDI child?
Any clue or explanation is welcome.
I'm learning pyqt5, and specifically how to use it with the QT Designer. I'm sort of following the turorial HERE. However in this tutorial they are converting the XML interface to Python code with pyuic5, while I'm trying to import it dynamically with uic.loadUi("myui.ui"). In the tutorial we define a slot with the signals and slot editor named " browseSlot".
When I try to run/compile, at the line
dlg = uic.loadUi("myui.ui")
I get the error:
AttributeError: 'QMainWindow' object has no attribute 'browseSlot'
I think what's going on is that QT Designer connects a signal to the slot 'browseSlot' but because a 'browseSlot' method isn't defined in the myui.ui, the error is thrown, because there is no way for the interpreter to know I'm referring to a method that is outside the UI interface file. (In this case, in the module that loads the interface). As far as I can tell QT Designer only lets me connect signals to slots, not define a whole new one. I think that way this is handled in other frameworks is that there will be an abstract method that needs over riding. So what can I do in this situation to make it work?
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import QObject, pyqtSlot
import sys
app = QtWidgets.QApplication([])
dlg = uic.loadUi("myui.ui")
#pyqtSlot
def returnPressedSlot():
pass
#pyqtSlot
def writeDocSlot():
pass
#pyQt
def browseSlot():
pass
dlg.show()
sys.exit(app.exec())
The slots belong to the class that is used returns loadUi(), they are not any functions since they do not magically not connect them, if you want to use loadUi() and implement these methods you must inherit from the class corresponding to the template that you used, in the example of the link Main Window was used so it must be inherited from QMainWindow:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
uic.loadUi("mainwindow.ui", self)
#QtCore.pyqtSlot()
def returnPressedSlot():
pass
#QtCore.pyqtSlot()
def writeDocSlot():
pass
#QtCore.pyqtSlot()
def browseSlot():
pass
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
try this out
from PyQt5 import QtWidgets, uic
app = QtWidgets.QApplication([])
form = uic.loadUi("login.ui")
form2.show()
app.exec()
the above python code should display your gui app properly as long as you have install PyQt5 and PyQt5-tools,if you haven't then open CMD and typeenter code here "pip install PyQt5" and click enter.once installation is done type "pip install PyQt5-tools" then you are good to go
When a QWebEngineView is added to the window, it causes strange scaling issues across the entire window. This is seen with the latest version of PyQt5 from pip (PyQt5=5.9, Qt5=5.9.1), Python 3.6, and Windows 10. For example:
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5 import QtWebEngineWidgets
#
class WebViewer(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
page = QtWebEngineWidgets.QWebEnginePage(self)
self.setPage(page)
self.setUrl(QtCore.QUrl('http://apple.com'))
#
#
if __name__ == '__main__':
appQT = QtWidgets.QApplication([])
#
main_widget = QtWidgets.QWidget(None)
window_layout = QtWidgets.QVBoxLayout(main_widget)
#
window_layout.addWidget(QtWidgets.QTextEdit("1. abc<br/>2. def<br/>3. ghi", main_widget))
####window_layout.addWidget(WebViewer(main_widget))
#
main_widget.show()
appQT.exec_()
#
Results in:
If I uncomment the window_layout.addWidget(WebViewer(main_widget)) line, I get:
The mouse position also does not correspond with what Qt thinks it's clicking on. Is this a bug in Qt5, or is there some dpi/scaling setting I should change? This is also seen with PyQt5.7.1 and PyQt5.8. This doesn't occur with a QWebView in older versions of PyQt5.
QObject.inherits(className) does work different in PyQt5 than in PyQt4 and PySide.
from PyQt5 import QtWidgets
#from PySide import QtGui
#from PyQt4 import QtGui
QtWidgets = QtGui
class MyWidget(QtWidgets.QWidget):
pass
app = QtWidgets.QApplication([])
w = MyWidget()
print(w.inherits("MyWidget"))
In PyQt5 it prints False, while in PyQt4 and PySide (uncomment the second or third line and comment the first one) it prints True. Why is that and how to fix it?
I can confirm this behaviour in PyQt-5.7.
It seems to be a bug, because the same problem does not appear in the latest development snapshot (PyQt5_gpl-5.7.1.dev1611251257). The only solution is to wait until PyQt-5.7.1 is released.
I've been learning python recently and now I wanted to (try to) create my first real application, a subtitle player for Linux. So far I've been using the Greenfish subtitle player, which is aimed at Windows users and not properly working in Linux.
I wanted to create the application in qt, since I discovered that transparent windows are not possible in tkinter, but if anybody knows a better framework please suggest!
Now before starting I've been researching the web for several hours to discover how to get my application to show over a full screened flash video and it seems like this is not possible. However the aforementioned GF subtitle player manages to do so in Windows, but not in Linux(maybe it's also because it's running through wine).
So my question is it possible to create a transparent application that remains over a fullscreened flash video and if so, could you point me in the right direction?
Thanks in advance.
edit:
here some example code I've been trying. The window produced by this piece of code does not stay above a fullscreened video
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
mywindow.show()
Update for PyQt5 pip install PyQt5
import sys
from PyQt5 import QtGui, QtCore, uic
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(
QtWidgets.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtWidgets.qApp.desktop().availableGeometry()
))
def mousePressEvent(self, event):
QtWidgets.qApp.quit()
if __name__ == '__main__':
app = QApplication(sys.argv)
mywindow = MainWindow()
mywindow.show()
app.exec_()
The example code below will create a centred, frameless window that should stay on top of all other windows on Linux (you can click on the window to close it).
import sys
from PyQt4 import QtGui, QtCore
class mymainwindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowFlags(
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.setGeometry(QtGui.QStyle.alignedRect(
QtCore.Qt.LeftToRight, QtCore.Qt.AlignCenter,
QtCore.QSize(220, 32),
QtGui.qApp.desktop().availableGeometry()))
def mousePressEvent(self, event):
QtGui.qApp.quit()
app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()