This means that every widget whose mandatoryField Qt property is set to true would have a yellow background.,Then, for each mandatory field widget, we would simply create a mandatoryField property on the fly and set it to true. For example:,Customizing Using Dynamic Properties,Let’s start by setting yellow as the background color of all QLineEdit s in an application. This could be achieved like this:
qApp.setStyleSheet("QLineEdit { background-color: yellow }")
myDialog.setStyleSheet("QLineEdit { background-color: yellow }")
myDialog.setStyleSheet("QLineEdit#nameEdit { background-color: yellow }")
nameEdit.setStyleSheet("background-color: yellow")
nameEdit.setStyleSheet("color: blue; background-color: yellow")
nameEdit.setStyleSheet("color: blue;"
"background-color: yellow;"
"selection-color: yellow;"
"selection-background-color: blue;")
In this chapter we will learn how to use the Qt framework classes in our Ring applications to create Desktop and Mobile Applications.,In this example we will learn about creating menubar and setting the window stylesheet,In this example we will learn about drawing using the QPainter class,In this example we will learn about using the QTreeWidget and QTreeWidgetItem classes
Load "guilib.ring"
MyApp = New qApp {
win1 = new qWidget() {
setwindowtitle("Hello World")
setGeometry(100, 100, 370, 250)
label1 = new qLabel(win1) {
settext("What is your name ?")
setGeometry(10, 20, 350, 30)
setalignment(Qt_AlignHCenter)
}
btn1 = new qpushbutton(win1) {
setGeometry(10, 200, 100, 30)
settext("Say Hello")
setclickevent("pHello()")
}
btn1 = new qpushbutton(win1) {
setGeometry(150, 200, 100, 30)
settext("Close")
setclickevent("pClose()")
}
lineedit1 = new qlineedit(win1) {
setGeometry(10, 100, 350, 30)
}
show()
}
exec()
}
Func pHello
lineedit1.settext("Hello " + lineedit1.text())
Func pClose
MyApp.quit()
Load "guilib.ring"
MyApp = New qApp {
win1 = new qWidget() {
setwindowtitle("Hello World")
setGeometry(100, 100, 400, 130)
label1 = new qLabel(win1) {
settext("What is your name ?")
setGeometry(10, 20, 350, 30)
setalignment(Qt_AlignHCenter)
}
btn1 = new qpushbutton(win1) {
setGeometry(10, 200, 100, 30)
settext("Say Hello")
setclickevent("pHello()")
}
btn2 = new qpushbutton(win1) {
setGeometry(150, 200, 100, 30)
settext("Close")
setclickevent("pClose()")
}
lineedit1 = new qlineedit(win1) {
setGeometry(10, 100, 350, 30)
}
layout1 = new qVBoxLayout() {
addwidget(label1)
addwidget(lineedit1)
addwidget(btn1)
addwidget(btn2)
}
win1.setlayout(layout1)
show()
}
exec()
}
Func pHello
lineedit1.settext("Hello " + lineedit1.text())
Func pClose
MyApp.quit()
Load "guilib.ring"
New qApp {
win1 = new qWidget() {
setwindowtitle("QTextEdit Class")
setGeometry(100, 100, 500, 500)
new qtextedit(win1) {
setGeometry(10, 10, 480, 480)
}
show()
}
exec()
}
Load "guilib.ring"
New qApp {
win1 = new qWidget() {
setGeometry(100, 100, 400, 400)
list1 = new qlistwidget(win1) {
setGeometry(150, 100, 200, 200)
alist = ["one", "two", "three", "four", "five"]
for x in alist additem(x) next
setcurrentrow(3, 2)
win1.setwindowtitle("Items Count : " + count())
}
btn1 = new qpushbutton(win1) {
setGeometry(10, 200, 100, 30)
settext("selected item")
setclickevent("pWork()")
}
btn2 = new qpushbutton(win1) {
setGeometry(10, 240, 100, 30)
settext("Delete item")
setclickevent("pWork2()")
}
show()
}
exec()
}
func pWork
btn1.settext(string(list1.currentrow()))
func pWork2
list1 {
takeitem(currentrow())
}
Load "guilib.ring"
New qApp {
win1 = new qWidget() {
setGeometry(100, 100, 500, 400)
list1 = new qlistwidget(win1) {
setGeometry(150, 100, 200, 200)
alist = ["one", "two", "three", "four", "five"]
for x in alist additem(x) next
setcurrentrow(3, 2)
win1.setwindowtitle("Items Count : " + count())
}
btn1 = new qpushbutton(win1) {
setGeometry(10, 200, 100, 30)
settext("selected item")
setclickevent("pWork()")
}
btn2 = new qpushbutton(win1) {
setGeometry(10, 240, 100, 30)
settext("Delete item")
setclickevent("pWork2()")
}
show()
}
exec()
}
func pWork
nbrOfItems = list1.count()
curItemNbr = list1.currentrow()
curValue = list1.item(list1.currentrow()).text()
win1.setwindowtitle("After Select - NbrOfItems: " + nbrOfItems +
" CurItemNbr: " + curItemNbr + " CurValue: " + curValue)
btn1.settext(string(list1.currentrow()) + " --- " +
list1.item(list1.currentrow()).text())
func pWork2
list1 {
takeitem(currentrow())
nbrOfItems = count()
curItemNbr = currentrow()
curValue = item(currentrow()).text()
win1.setwindowtitle("After Delete - NbrOfItems: " + nbrOfItems +
" CurItemNbr: " + curItemNbr + " CurValue: " + curValue)
}
Load "guilib.ring"
New qApp {
win1 = New qWidget() {
setwindowtitle("Using QTreeView and QFileSystemModel")
setGeometry(100, 100, 500, 400)
New qtreeview(win1) {
setGeometry(00, 00, 500, 400)
oDir = new QDir()
ofile = new QFileSystemModel()
ofile.setrootpath(oDir.currentpath())
setmodel(ofile)
}
show()
}
exec()
}
#ifndef BORDERIMAGE_H#define BORDERIMAGE_H #include <QtGui> class borderImage : public QObject, public QImage{ Q_OBJECT Q_PROPERTY(int alpha READ alpha WRITE setAlpha)public: borderImage(int width, int height, QWidget *parent = 0, const QString & filename = NULL); borderImage(QImage image, QObject *parent=0); QImage * getImage() { return m_out ;} void setDistances(int top, int right, int bottom, int left); void draw(int w, int h, float b_w); int alpha() const {return m_alpha;} void setAlpha(int alpha); private: int m_top, m_right, m_bottom, m_left, m_w, m_h; int m_alpha; QImage *m_out; QImage *m_original;}; #endif // BORDERIMAGE_H
#ifndef BORDERIMAGE_H#define BORDERIMAGE_H #include <QtGui> class borderImage : public QObject, public QImage{ Q_OBJECT Q_PROPERTY(int alpha READ alpha WRITE setAlpha)public: borderImage(int width, int height, QWidget *parent = 0, const QString & filename = NULL); borderImage(QImage image, QObject *parent=0); QImage * getImage() { return m_out ;} void setDistances(int top, int right, int bottom, int left); void draw(int w, int h, float b_w); int alpha() const {return m_alpha;} void setAlpha(int alpha); private: int m_top, m_right, m_bottom, m_left, m_w, m_h; int m_alpha; QImage *m_out; QImage *m_original;}; #endif // BORDERIMAGE_H
#ifndef BORDERIMAGE_H
#define BORDERIMAGE_H
#include <QtGui>
class borderImage : public QObject, public QImage
{
Q_OBJECT
Q_PROPERTY(int alpha READ alpha WRITE setAlpha)
public:
borderImage(int width, int height, QWidget *parent = 0, const QString & filename = NULL);
borderImage(QImage image, QObject *parent=0);
QImage * getImage()
{ return m_out ;}
void setDistances(int top, int right, int bottom, int left);
void draw(int w, int h, float b_w);
int alpha() const
{return m_alpha;}
void setAlpha(int alpha);
private:
int m_top, m_right, m_bottom, m_left, m_w, m_h;
int m_alpha;
QImage *m_out;
QImage *m_original;
};
#endif // BORDERIMAGE_H
#ifndef MAINWIDGET_H#define MAINWIDGET_H #include <QWidget>#include "borderimage.h" class mainWidget : public QWidget{ Q_OBJECT public: mainWidget(QWidget *parent = 0); ~mainWidget(); borderImage * getBorde() { return borde; } protected: void paintEvent(QPaintEvent *event); public slots: void buttonClicked(); void timerEvent(QTimerEvent *event); private: void loadPlugins(QGridLayout *layout); borderImage *borde; QImage background; QObjectList plugins;}; #endif // MAINWIDGET_H
#ifndef MAINWIDGET_H#define MAINWIDGET_H #include <QWidget>#include "borderimage.h" class mainWidget : public QWidget{ Q_OBJECT public: mainWidget(QWidget *parent = 0); ~mainWidget(); borderImage * getBorde() { return borde; } protected: void paintEvent(QPaintEvent *event); public slots: void buttonClicked(); void timerEvent(QTimerEvent *event); private: void loadPlugins(QGridLayout *layout); borderImage *borde; QImage background; QObjectList plugins;}; #endif // MAINWIDGET_H
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include "borderimage.h"
class mainWidget : public QWidget
{
Q_OBJECT
public:
mainWidget(QWidget *parent = 0);
~mainWidget();
borderImage * getBorde() { return borde; }
protected:
void paintEvent(QPaintEvent *event);
public slots:
void buttonClicked();
void timerEvent(QTimerEvent *event);
private:
void loadPlugins(QGridLayout *layout);
borderImage *borde;
QImage background;
QObjectList plugins;
};
#endif // MAINWIDGET_H
borderImage::borderImage(int width, int height, QWidget *parent, const QString & filename ): QObject(parent), QImage(width, height, Format_ARGB32){ if (filename!=NULL) { m_original = new QImage(filename); m_out = new QImage(filename); } this->fill(Qt::transparent); //this->setDotsPerMeterX(10); //this->setDotsPerMeterY(10);} ... void borderImage::setAlpha(int alpha){ int i; int j; QColor color; for (i=0;i< (m_original->size().height());++i) { const QRgb * rgb = (QRgb *) m_original->scanLine(i); QRgb * nuevo = (QRgb *) m_out->scanLine(i); for (j=1; j<m_out->bytesPerLine();++j) { color.setRgba(*rgb); if (color.alpha()>alpha) { color.setAlpha(alpha); //color.toRgb(); //color.setRedF(color.redF()*alpha/255); //color.setGreenF(color.greenF()*alpha/255); //color.setBlueF(color.blueF()*alpha/255); *nuevo = color.rgba(); } ++rgb; ++nuevo; } } m_alpha=alpha; //cout << alpha << "\n"; QWidget *papa = qobject_cast<QWidget *>(this->parent()); papa->update();}
borderImage::borderImage(int width, int height, QWidget *parent, const QString & filename ): QObject(parent), QImage(width, height, Format_ARGB32){ if (filename!=NULL) { m_original = new QImage(filename); m_out = new QImage(filename); } this->fill(Qt::transparent); //this->setDotsPerMeterX(10); //this->setDotsPerMeterY(10);} ... void borderImage::setAlpha(int alpha){ int i; int j; QColor color; for (i=0;i< (m_original->size().height());++i) { const QRgb * rgb = (QRgb *) m_original->scanLine(i); QRgb * nuevo = (QRgb *) m_out->scanLine(i); for (j=1; j<m_out->bytesPerLine();++j) { color.setRgba(*rgb); if (color.alpha()>alpha) { color.setAlpha(alpha); //color.toRgb(); //color.setRedF(color.redF()*alpha/255); //color.setGreenF(color.greenF()*alpha/255); //color.setBlueF(color.blueF()*alpha/255); *nuevo = color.rgba(); } ++rgb; ++nuevo; } } m_alpha=alpha; //cout << alpha << "\n"; QWidget *papa = qobject_cast<QWidget *>(this->parent()); papa->update();}
borderImage::borderImage(int width, int height, QWidget *parent, const QString & filename ):
QObject(parent), QImage(width, height, Format_ARGB32)
{
if (filename!=NULL)
{
m_original = new QImage(filename);
m_out = new QImage(filename);
}
this->fill(Qt::transparent);
//this->setDotsPerMeterX(10);
//this->setDotsPerMeterY(10);
}
...
void borderImage::setAlpha(int alpha)
{
int i;
int j;
QColor color;
for (i=0;i< (m_original->size().height());++i)
{
const QRgb * rgb = (QRgb *) m_original->scanLine(i);
QRgb * nuevo = (QRgb *) m_out->scanLine(i);
for (j=1; j<m_out->bytesPerLine();++j)
{
color.setRgba(*rgb);
if (color.alpha()>alpha)
{
color.setAlpha(alpha);
//color.toRgb();
//color.setRedF(color.redF()*alpha/255);
//color.setGreenF(color.greenF()*alpha/255);
//color.setBlueF(color.blueF()*alpha/255);
*nuevo = color.rgba();
}
++rgb;
++nuevo;
}
}
m_alpha=alpha;
//cout << alpha << "\n";
QWidget *papa = qobject_cast<QWidget *>(this->parent());
papa->update();
}
I have a simple Qt window it has a button and when you click the button it opens a explorer window for a specific location, when the notification fades out the window closes as well.,I’m not able to recreate this in my Python 3.5 environment. If you need help, could you please provide the PyQt version you are using and also the imports.,– Probably the issue here is that this module doesn’t interface with the Qt event loop. @author, you might be able to fix this blocking issue by periodically taking breaks in a loop (that ultimately waits the entire 5 seconds, or however long) to call QApplication.processEvents().,Heyo. Commenting out that line seemed to fix the crashing. The sleep(duration) a few lines before that hangs the program for a few seconds so I commented that out also.
Entire Example Script `class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')
subprocess.Popen(r'explorer /open,"C:\WB Resources\"')
toaster = ToastNotifier()
toaster.show_toast("TITLE",
"Python is 10 seconds awsm! and I think that this is the way to go for all information going forward",
icon_path="wbicon.ico",
duration=3)
print('Finished?')
def on_click_cache(self):
path = "C:/Users/XXXXX/AppData/Local/Packages/XXXXXXXXXXXX_XXXXXXXXXXXX/LocalState/cacheddata/"
try:
shutil.rmtree(path)
except:
print('No folder')
self.btn_clearcache.setStyleSheet("background-color: green")
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
btn = QPushButton('Open WBR',self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(150,50)
btn.move(0, 0)
btn.clicked.connect(self.on_click)
self.btn_clearcache = QPushButton('Clear Cache', self)
self.btn_clearcache.setToolTip('Clear the WB App Cache<br><b>THIS IS DESTRUCTIVE</b>')
self.btn_clearcache.resize(150,50)
self.btn_clearcache.move(151, 0)
self.btn_clearcache.setStyleSheet("QPushButton:focus:pressed { background-color: red }")
self.btn_clearcache.clicked.connect(self.on_click_cache)
self.setGeometry(500, 350, 900, 500)
self.setWindowTitle('WB-TOOL')
self.setWindowIcon(QIcon('arrow.png'))
self.show()
Note: argc and argv might be changed as Qt removes command line arguments that it recognizes.,-bg or -background color, sets the default background color and an application palette (light and dark shades are calculated).,-fg or -foreground color, sets the default foreground color.,Initializes the window system and constructs an application object with argc command line arguments in argv.
The following example shows how to create an application that uses a graphical interface when available.
int main(int argc, char ** argv) {
#ifdef Q_WS_X11
bool useGUI = getenv("DISPLAY") != 0;
#else
bool useGUI = true;
#endif
QApplication app(argc, argv, useGUI);
if (useGUI) {
// start GUI version
...
} else {
// start non-GUI version
...
}
return app.exec();
}
Example:
void updateAllWidgets() {
foreach(QWidget * widget, QApplication.allWidgets())
widget - > update();
}
exitAct = new QAction(tr("E&xit"), this);
exitAct - > setShortcuts(QKeySequence.Quit);
exitAct - > setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));