QPainter/Python - Graphic display issue [duplicate] - pyqt

I notice that when I use a QPixmap inside a QLabel, seemingly random pixels (possibly based on memory) are written to the QPixmap. Why is this, and how can this be fixed? Is this just a problem with my computer? (I use Windows 7, by the way.)
import sys
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel
class BugTest(QLabel):
def __init__(self):
super().__init__()
self.setPixmap(QPixmap(200, 200))
self.show()
app = QApplication(sys.argv)
widget = BugTest()
app.exec_()

The documentation says:
Warning: This will create a QPixmap with uninitialized data. Call fill() to fill the pixmap with an appropriate color before drawing onto it with QPainter.
That means it may contain junk. If you want it to be all black, fill it with black.

Related

How to use a custom GTK widget written in Vala from Python?

Recently i managed to make a shared library that i could use from this Vala file with these commands:
1:valac circular-progress-bar.vala -X -fPIC -X -shared -o test_shared.so --library=testShared --gir testShared-0.1.gir --pkg gtk+-3.0
2: g-ir-compiler --shared-library=test_shared.so --output=testShared-0.1.typelib testShared-0.1.gir
I made a simple test window in python which shows the widget and it only shows the text. Is it Python or it simply can't be used in this way ?
Image of the test window/app
I tried finding function to change settings or some value but i can't find it.
I would appreciate any help given !
After you've gone through the difficult technical steps of compiling an object and generating interface files to use the widget in Python, this answer may be a bit of a shock. As far as I can tell the reason no circle is showing is because the value is 0% - so no circle!
The widget has a percentage property and this can be set in Python with pb.props.percentage. In this example the percentage is set to 60% and works fine for me. I use pb.props.percentage = 0.6:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GObject
gi.require_version('testShared', '0.1')
from gi.repository import testShared
class GUI (Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
pb = testShared.CircularProgressBar()
pb.props.percentage = 0.6
self.connect('destroy', self.on_window_destroy)
self.add(pb)
self.show_all()
Gtk.main()
def on_window_destroy(self, window):
Gtk.main_quit()
if __name__ == "__main__":
GUI()
The widget has other properties that can be changed, e.g. line_width sets the width of the line that draws the circle.
Here is a screenshot showing the 60% and the line_width set to 10:

How to setFocus() on QListView in QFileDialog in PyQt5?

I've made my first small GUI program using PyQt5 for some data process for my work.
I have a problem - I can't make QFileDialog (to open file) with the focus on its QListView widget. (In QFileDialog class by default focus is on QLineEdit).
The best solution is to make new class from QFileDialog with changed setFocus settings and unchanged all the other.
It turned out to be not so easy because I didn't find how to adress to inner widgets of QFileDialog.
I'm new in PyQt5, couldn't find any solutionts even for Qt C++.
Thank you for any advices and ideas.
class XFileDialog(QtWidgets.QFileDialog):
"magic code"
file=XFileDialog.getOpenFileName(caption="Open",
filter="FITS (*.fits *.fts *.new)")
It seems that I have found the solution. Perhaps it could be useful for someone.
class XFileDialog(QtWidgets.QFileDialog):
def __init__(self):
QtWidgets.QFileDialog.__init__(self)
self.setDirectory(progdir)
def setVisible(self,v):
super(XFileDialog, self).setVisible(v)
self.setAcceptMode(0)
self.setFileMode(1)
self.setFocusPolicy(11)
self.setNameFilter("All (*) ;; FITS (*.fts *.fits *.new)")
self.focusPreviousChild()
class MyWindow(QtWidgets.QWidget):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent)
super().__init__()
self.initUI()
def openFile(self):
global progdir
progdir=QtCore.QDir(os.getcwd())
file=XFileDialog()
file.exec()
.......

grab image from clipbroad fuction pyhon in linux

I am looking for a method in which I can grab the image which is on the clipboard and assign it to the background of a pygame screen. I am trying to make an app in which a user can quickly annotate a captured image in linux. Below is a an example of what I am talking about. Unfortunately these modules do not work in linux. It's not the complete code and I have just included it to make my question clearer.
import pygame
from PIL import ImageTk, ImageGr
pygame.init()
def backgroundimage():
while True:
clipimage = ImageGrab.grabclipboard()
screen = pygame.display.set_mode(([800,800]), pygame.NOFRAME)
screen.fill((clipimage))
if __name__ == '__main__':
backgroundimage()

Implementing web inspection in browser using PyQt5

QtWebKit is no longer supported in PyQt5.
Although there are alternates to some of the classes of QtWebKit in QtWebEngineWidgets. But, i couldn't find any alternate to the QWebInspector class that is available in PyQt4.
Are there any such classes OR even any other option so that i can implement web inspector using PyQt5 ?
Edit: Qt5.6 and beyond has removed QtWebKitWidgets
I was somewhat surprised to find that QtWebKit is making a comeback. It is still not part of Qt-5.6 or Qt-5.7, but it seems that it may continue to be maintained as separate project. This means PyQt5 can continue to support QtWebKit, even though the official Qt5 docs say it has been removed.
Depending on your platform, this probably means you will need to install some extra packages if you want to use the "new" QtWebKit module in PyQt5.
PS:
As for QtWebEngine - if you're using ubuntu/debian, it seems you will have to wait for it to be supported. See Bug #1579265.
I show the following example to use QWebInspector in PyQt5 version 5.7.1
from PyQt5.QtCore import QUrl
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
from PyQt5.QtWidgets import QApplication, QSplitter, QVBoxLayout, QWidget
class Window(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self.view = QWebView(self)
self.view.settings().setAttribute(
QWebSettings.DeveloperExtrasEnabled, True)
self.inspector = QWebInspector()
self.inspector.setPage(self.view.page())
self.inspector.show()
self.splitter = QSplitter(self)
self.splitter.addWidget(self.view)
self.splitter.addWidget(self.inspector)
layout = QVBoxLayout(self)
layout.addWidget(self.splitter)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.view.load(QUrl('http://www.google.com'))
window.show()
sys.exit(app.exec_())

How to avoid mousePressEvent - left click to call paintEvent in PyQt

In this program below, I am testing the affect of mousePressEvent:
import sys
from PyQt4 import QtGui, Qt, QtCore
class Test(QtGui.QFrame):
def __init__(self):
QtGui.QFrame.__init__(self)
self.setGeometry(30,30,500, 500)
self.show()
def paintEvent(self, e=None):
print "paintEvent"
qp = QtGui.QPainter()
qp.begin(self)
qp.drawRect(30,30,80,80)
qp.end()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.RightButton:
print "mousePressEvent- Right"
else:
print "mousePressEvent- Left"
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_())
I see that in the first left-click, the frame's paintEvent is called. Is this because when the frame get the focus, it need to be repainted? I wonder if there is any way to avoid paintEvent to be called and every drawing in the frame still intact. The reason is because in my real program, the paintEvent is really heavy, I want to run it as less times as possible.
If that is not possible, is there a way to avoid the frame getting focus when left-click on that?
I don't know whether there are platform-specific differences at play here, but when I try the example code on Linux, there is no paintEvent when clicking on the frame.
This is not surprising really, because, by default, the QFrame is not configured to accept focus of any kind. To get the example to work, I had to explicitly set the focus policy, like this:
class Test(QtGui.QFrame):
def __init__(self):
QtGui.QFrame.__init__(self)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
But maybe the defaults are somehow different on other platforms, and so, to explicitly prevent the frame getting the focus, you might need to do this:
self.setFocusPolicy(QtCore.Qt.NoFocus)

Resources