I need to going to some directory when user click on button.
User enter path in QInputDialog (for example /mnt/data/Music on linux), and program go to this directory, like M-c in Midnight Commander or cd in ranger, but i don't know how i can do this... I already know all the documentation by heart
main.py
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QMainWindow, QInputDialog
import exampleQTV
class PyMap(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.search.clicked.connect(self.searchAction)
def searchAction(self):
text, ok = QInputDialog.getText(self, "Go to", "Enter path")
if ok:
path = text.split("/")
for item in path:
if item != "":
print("ITEM:", item)
self.leftView.keyboardSearch(item)
self.leftView.setExpanded(self.leftView.currentIndex(), True)
print("currentIndex():",self.leftView.model.filePath(self.leftView.currentIndex()))
child = self.leftView.currentIndex().child(0,0)
print("child:", self.leftView.model.filePath(child))
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.leftView = exampleQTV.exampleQTV()
self.leftView.setObjectName("leftView")
self.gridLayout.addWidget(self.leftView, 1, 0, 1, 1)
self.search = QtWidgets.QPushButton(self.centralwidget)
self.search.setObjectName("select")
self.gridLayout.addWidget(self.search, 0, 0, 1, 2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.search.setText(_translate("MainWindow", "Search"))
def main():
app = QtWidgets.QApplication(sys.argv)
window = PyMap()
window.show()
app.exec_()
if __name__ == '__main__':
main()
exampleQTV.py
from PyQt5.QtWidgets import QTreeView, QFileSystemModel
from PyQt5.QtCore import QDir
class exampleQTV(QTreeView):
def __init__(self):
QTreeView.__init__(self)
self.model = QFileSystemModel()
self.model.setRootPath(QDir.rootPath())
self.setModel(self.model)
self.eventCalled = False
self.requestForEscape = False
self.setColumnHidden(1, True)
self.setColumnHidden(2, True)
self.setColumnHidden(3, True)
self.expandAll()
If you want an item to be expanded your ancestors must be expanded, in the following code you implement that logic:
import sys
from PyQt5 import QtCore, QtWidgets
def expandPath(index, view):
if not index.isValid():
return
indexes = []
ix = index
while ix.isValid():
indexes.insert(0, ix)
ix = ix.parent()
for ix in indexes:
view.expand(ix)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
model = QtWidgets.QFileSystemModel()
model.setRootPath(QtCore.QDir.rootPath())
view = QtWidgets.QTreeView()
view.setModel(model)
view.setRootIndex(model.index(QtCore.QDir.rootPath()))
doc_directory = QtCore.QStandardPaths.writableLocation(
QtCore.QStandardPaths.DocumentsLocation
)
ix = model.index(doc_directory)
expandPath(ix, view)
view.resize(640, 480)
for i in (1, 2, 3):
view.setColumnHidden(i, True)
view.show()
sys.exit(app.exec_())
Related
How can I emit a signal (or for now print the text) whenever the text from any QComboBox changes? A new QComboBox is created in both columns every time a row is added to a QTableWidget. Eventually, that signal will trigger a redraw of a plot.
Right now, I'm able to print the text from a QComboBox that already exists with a specific instance name, but I don't know how to do it when combo boxes are created dynamically in the table.
Main code:
import sys
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QComboBox,
)
from cb_in_tables import Ui_MainWindow
class MyWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.comboBox.addItems(['', '1', '2', '3'])
self.ui.pushButton.clicked.connect(self.addRow)
self.ui.comboBox.currentTextChanged.connect(self.printComboBoxText)
def printComboBoxText(self, text):
print(text)
def addRow(self):
"""Add new row to data table and populate with widgets."""
# Insert row
rowPos = self.ui.tableWidget.rowCount()
self.ui.tableWidget.insertRow(rowPos)
x_combobox = QComboBox()
x_combobox.addItems(['', 'A', 'B', 'C'])
self.ui.tableWidget.setCellWidget(rowPos, 0, x_combobox)
y_combobox = QComboBox()
y_combobox.addItems(['', 'X', 'Y', 'Z'])
self.ui.tableWidget.setCellWidget(rowPos, 1, y_combobox)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MyWindow()
main.show()
sys.exit(app.exec_())
UI file:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'cb_in_tables.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(291, 299)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(30, 60, 231, 161))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(2)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 20, 113, 32))
self.pushButton.setObjectName("pushButton")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(160, 20, 104, 31))
self.comboBox.setObjectName("comboBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 291, 24))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "X"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "Y"))
self.pushButton.setText(_translate("MainWindow", "Add Row"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Nothing changes:
x_combobox.currentTextChanged.connect(self.printComboBoxText)
y_combobox.currentTextChanged.connect(self.printComboBoxText)
I have created 2 windows, win1 and win2, using qt designer and have added two buttons: btn_open_win2 to open win2 from win1 and btn_close to close win2.
When I run win2 directly the close method works correctly but when I run win1 and call win2 from it, the close button on win2 doesn't operate correctly.
I know that the problem arises from the point that win2 has been defined inside the if __name__ =='__main__' part as shown in the below codes but I can't solve this problem.
win1:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win1(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Win1")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_open_win2 = QtWidgets.QPushButton(self.centralwidget)
self.btn_open_win2.setGeometry(QtCore.QRect(250, 170, 131, 101))
self.btn_open_win2.setObjectName("btn_open_win2")
self.btn_open_win2.clicked.connect(self.on_open_win2)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Win1"))
self.btn_open_win2.setText(_translate("MainWindow", "open win2"))
def on_open_win2(self):
from win2 import Ui_Win2
self.win = QMainWindow()
self.ui = Ui_Win2()
self.ui.setupUi(self.win)
self.win.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win1 = QMainWindow()
ui = Ui_Win1()
ui.setupUi(win1)
win1.show()
sys.exit(app.exec_())
win2:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win2(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Win2")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_close = QtWidgets.QPushButton(self.centralwidget)
self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
self.btn_close.setObjectName("btn_close")
self.btn_close.clicked.connect(self.on_close)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Win2"))
self.btn_close.setText(_translate("MainWindow", "close"))
def on_close(self):
# win2 = QMainWindow()
# ui = Ui_Win2()
# ui.setupUi(win2)
win2.close()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win2 = QMainWindow()
ui = Ui_Win2()
ui.setupUi(win2)
win2.show()
sys.exit(app.exec_())
change win2 like this:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class Ui_Win2(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
MainWindow.setObjectName("Win2")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_close = QtWidgets.QPushButton(self.centralwidget)
self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
self.btn_close.setObjectName("btn_close")
self.btn_close.clicked.connect(self.on_close)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Win2"))
self.btn_close.setText(_translate("MainWindow", "close"))
def on_close(self):
# win2 = QMainWindow()
# ui = Ui_Win2()
# ui.setupUi(win2)
self.MainWindow.close()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win2 = QMainWindow()
ui = Ui_Win2()
ui.setupUi(win2)
win2.show()
sys.exit(app.exec_())
call close function from self.MainWindow instead of self. and set self.MainWindow in first line of setupUi function
I am trying to remove QTextDocument which is inside the QTextEdit but I am not able to do it. I used clear() method of QTextEdit but it won't clear the QTextEdit area. I also try to overwrite new document on previous by setting overwrite flag of QTextEdit but that doesn't work as well.
def create_doc(body_text):
doc = QTextDocument()
doc.setDocumentMargin(10)
cursor = QTextCursor(doc)
cursor.movePosition(QTextCursor.Start)
block_fmt = QTextBlockFormat()
block_fmt.setBottomMargin(6)
block_fmt.setAlignment(QtCore.Qt.AlignJustify)
para_fmt = QTextCharFormat()
para_fmt.setFontWeight(QFont.Normal)
para_fmt.setFontPointSize(12)
para_fmt.setFontFamilies(["Raleway", "Arial"])
cursor.insertBlock(block_fmt)
cursor.insertText(body_text, para_fmt)
cursor.movePosition(QTextCursor.End)
return doc
class AppUI(QtWidgets.QMainWindow):
def __init__(self):
super(AppUI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.qTextEditArea.setOverwriteMode(True)
self.ui.button.clicked.connect(lambda : self.add_text("some text")
def add_text(self, text):
self.ui.qTextEditArea.clear()
document = create_doc(doc)
self.ui.qTextEditArea.setDocument(document)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ui = AppUI()
ui.show()
sys.exit(app.exec_())
If you know how to do it please help. Thanks
QTextEdit Class
document : QTextDocument*
This property holds the underlying document of the text editor.
Note: The editor does not take ownership of the document unless it is the document's parent object. The parent object of the provided document remains the owner of the object. If the previously assigned document is a child of the editor then it will be deleted.
Access functions:
QTextDocument * document() const
void setDocument(QTextDocument *document)
main.py
import sys
from PyQt5.Qt import *
from textEditArea import Ui_MainWindow
def create_doc(body_text):
doc = QTextDocument()
doc.setDocumentMargin(10)
cursor = QTextCursor(doc)
cursor.movePosition(QTextCursor.Start)
block_fmt = QTextBlockFormat()
block_fmt.setBottomMargin(6)
block_fmt.setAlignment(Qt.AlignJustify)
para_fmt = QTextCharFormat()
para_fmt.setFontWeight(QFont.Normal)
para_fmt.setFontPointSize(12)
para_fmt.setFontFamilies(["Raleway", "Arial"]) # This function was introduced in Qt 5.13.
cursor.insertBlock(block_fmt)
cursor.insertText(body_text, para_fmt)
cursor.movePosition(QTextCursor.End)
return doc
class AppUI(QMainWindow):
def __init__(self):
super(AppUI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# self.ui.qTextEditArea.setOverwriteMode(True)
self.ui.button.clicked.connect(lambda : self.add_text("some text"))
def add_text(self, text):
# self.ui.qTextEditArea.clear()
# document = create_doc(doc) # ??? (doc)
document = create_doc(text) # +++ text
self.ui.qTextEditArea.setDocument(document)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = AppUI()
ui.show()
sys.exit(app.exec_())
textEditArea.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(456, 304)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.qTextEditArea = QtWidgets.QTextEdit(self.centralwidget)
self.qTextEditArea.setObjectName("qTextEditArea")
self.gridLayout.addWidget(self.qTextEditArea, 0, 0, 1, 1)
self.button = QtWidgets.QPushButton(self.centralwidget)
self.button.setObjectName("button")
self.gridLayout.addWidget(self.button, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 456, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button.setText(_translate("MainWindow", "PushButton"))
I'm making a simple PyQt5 application and am curious about how to make the circle move with the arrow keys. I want it to move 5 pixels on every press.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 500)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.coords = QtWidgets.QPushButton(self.centralwidget)
self.coords.setGeometry(QtCore.QRect(0, 0, 75, 23))
self.coords.setObjectName("coords")
self.mover = QtWidgets.QLabel(self.centralwidget)
self.mover.setGeometry(QtCore.QRect(200, 200, 50, 50))
self.mover.setText("")
self.mover.setPixmap(QtGui.QPixmap("mover.png"))
self.mover.setObjectName("mover")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "DYM"))
self.coords.setText(_translate("MainWindow", "Coords"))
if __name__ == "__main__":
# Snip
You shouldn't edit nor use the code provided by pyuic from Designer files directly.
Instead, you have to create your own code that uses that code as explained here.
A very simple and limited implementation (which doesn't use Designer .ui files) looks like this:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Mover(QtWidgets.QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setGeometry(0, 0, 500, 21)
self.setPixmap(QtGui.QPixmap('mover.png'))
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Up:
self.move(self.x(), self.y() - 5)
elif event.key() == QtCore.Qt.Key_Down:
self.move(self.x(), self.y() + 5)
elif event.key() == QtCore.Qt.Key_Left:
self.move(self.x() - 5, self.y())
elif event.key() == QtCore.Qt.Key_Right:
self.move(self.x() + 5, self.y())
else:
QtWidgets.QLabel.keyPressEvent(self, event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
centralWidget = QtWidgets.QWidget()
self.setCentralWidget(centralWidget)
self.mover = Mover(centralWidget)
self.mover.setFocus()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
There are some drawback about this, though: I had to setFocus to the widget in order to make it capture key press events, which means that if you have any other widget that interacts with key events (such as a QLineEdit) and it gets focus, your "mover" widget won't move anymore until it receives its focus back (which is usually achieved by means of a QWidget.setFocusPolicy()).
So I have this Ui_MainWindow class generated by pyuic5.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.canvas = QtWidgets.QWidget(self.centralWidget)
self.canvas.setGeometry(QtCore.QRect(70, 30, 621, 341))
self.canvas.setAutoFillBackground(False)
self.canvas.setObjectName("canvas")
self.gview = QtWidgets.QGraphicsView(self.canvas)
self.gview.setGeometry(QtCore.QRect(80, 50, 256, 192))
self.gview.setObjectName("gview")
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menuBar.setObjectName("menuBar")
self.menuhola = QtWidgets.QMenu(self.menuBar)
self.menuhola.setObjectName("menuhola")
self.menust = QtWidgets.QMenu(self.menuBar)
self.menust.setObjectName("menust")
MainWindow.setMenuBar(self.menuBar)
self.menuBar.addAction(self.menuhola.menuAction())
self.menuBar.addAction(self.menust.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuhola.setTitle(_translate("MainWindow", "hola"))
self.menust.setTitle(_translate("MainWindow", "splendid"))
And I want to paint on something, as long as it's not the main window. However, no matter what I try, it just doesn't work.
Here's my code:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPainter
from src.qt_design.main_window import Ui_MainWindow
class SystemApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def paintEvent(self, event):
# Nope
painter = QPainter(self.ui.gview)
painter.drawLine(0, 0, 50, 50)
# Nope
painter = QPainter(self.ui.canvas)
painter.drawLine(0, 0, 50, 50)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = SystemApp()
window.show()
sys.exit(app.exec_())
I'm not sure if I should either:
1. Paint with this parent on its children (like what I tried above)
2. Override children's paintEvent, but it's maybe a bad idea, see here
Any help will be appreciated!
You can get what you are trying to accomplish with an event filter
class SystemApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.gview.installEventFilter(self)
self.ui.canvas.installEventFilter(self)
def eventFilter(self, obj, e):
if e.type() == QtCore.QEvent.Paint:
painter = QPainter()
painter.begin(obj)
if obj == self.ui.canvas:
painter.setPen(QtCore.Qt.red) # Some specific painting
painter.drawLine(0, 0, 50, 50)
painter.end()
return True
return super().eventFilter(obj, e)