Linux Format - UK (2020-03)

(Antfer) #1
88 LXF260 March 2020 http://www.linuxformat.com

CODING ACADEMY Python & Qt5


of Figure 3. All other widgets should have their
focusPolicy set to StrongFocus. All widgets should have
their enabled check box checked and should also have
their toolTip and whatsThis text properties set to
something descriptive.
We now want to set the size policy of all of the
widgets so that the dialog will resize properly. The four
widgets on the upper left of the dialog and the two
QButtons have their horizontal and vertical size policies
set to Fixed. They won’t be resized. We want all the other
widgets to stretch when we resize the dialog. The Status
label has its horizontal and vertical size policies set to
Preferred. The two list widgets, listWidgetRepo and
listWidgetStatus, have their horizontal and vertical size
policies set to Minimum Expanding.
Select Form > Preview from the menu and make sure
that the dialog resizes properly when you stretch it by
pulling on the bottom right-hand corner of the dialog.
There are just a couple more steps. Select Edit > Edit
Buddies from the menu and make the Git Repositories
label a buddy of listWidgetRepo. Then make the Status
label a buddy of listWidgetStatus.
Now, select Edit > Edit Tab Order from the menu
and set the tab order to 1. listWidgetRepo,


  1. listWidgetStatus, 3. pushButtonRefresh and

  2. pushButtonClose.
    Finally, select Edit > Edit Signals/Slots and drag a
    line between pushButtonClose and the Close Button on
    the top right-hand corner of the gitStatus Dialog. Use
    the Configure Connection dialog to make the SIGNAL
    clicked(bool) connect to the SLOT accept(). This is


o you need a hand keeping all your git
repositories up to date? Do you want a visual
reminder of the modified, added, renamed,
untracked and deleted files in your git working
directories? Build a Qt 5 Python 3 GUI application that
finds all of your git repositories and displays their status
through colours.
The left panel of Figure 1 (see right) shows the
locations of all of the git working directories sorted
alphabetically. We have selected the git working
directory home/js/Development/historyDialog.
Notice that its colour is red, which indicates the
presence of modified files in the working directory.
There are also deleted and untracked files, but the
overall status is determined by the presence of modified
files. The right panel shows the issues that git reports
about individual files in the git working directory. They
are colour-coded by importance, where red signifies an
important issue, orange signifies a less-important issue
and green signifies that git reported no issues.
Our historyDialog directory shown in Figure 1 is not
currently used. It could be deleted, but we can also tell
gitStatus.py to ignore this repository by adding it to the
gitStatus.ini file in the same directory.
You’ll build the dialog-based application visually
using Qt 5 Designer and then save it as
GitStatusDialog.ui. This is an XML file that contains
information about all of the widgets in the application
and their properties and configurations. You’re going to
use a tool to convert it into a Python script.
Execute Qt 5 Designer from the system menu or
from the command line. In order to run it from the
command line type: designer-qt5.
You should see the File > New Dialog as shown in
Figure 2 (see opposite). You need to add the methods
shown in the boxout (see opposite). Build the dialog
from scratch by clicking on Dialog Without Buttons
under Templates/Forms. You can also, of course, start
with your copy of GitStatusDialog.ui.
If you’re building it from scratch, you will want to add
widgets from the palette found on the left; add three
QLabels, two QTextEdits, two QListWidgets and two
QButtons and position them as shown in Figure 3 (see
page 90). The upper-right corner of the screen in Figure
3 shows the added widgets and their assigned names.
The QLabels and the QTextEdit widgets should have
a focusPolicy of NoFocus selected. Set the properties of
these in the property editor, seen on the right-hand side

Figure 1: The gitStatus application has found the git repositories. The
Status pane shows issues reported by git for the historyDialog repository.

Monitor git projects


John Schwartzman discusses a PyQt5 program that will find all of


your git repositories and display their status.


John
Schwartzman
is a long-time
engineering
consultant to
business and
government. He
also teaches
Computer
Science at a
local college.
John can be
reached at john@
fortesystem.
com.

D


PYTHON


OUR
EXPERT

Running pydoc
./gitStatus.py
provides a list
of the methods
and data in
gitStatus.py
and allows
you to browse
it using less.
Running pydoc
./gitStatus.py
> gitStatus.txt
will produce
gitStatus.txt
you can browse
in your editor.

CODING ACADEMY


88 LXF260 March 2020 http://www.linuxformat.com

ofFigure3.Allotherwidgetsshouldhavetheir
focusPolicysettoStrongFocus.Allwidgetsshould have
theirenabledcheckboxcheckedandshouldalso have
theirtoolTipandwhatsThistextpropertiessetto
somethingdescriptive.
Wenowwanttosetthesizepolicyofallofthe
widgetssothatthedialogwillresizeproperly.The four
widgetsontheupperleftofthedialogandthetwo
QButtonshavetheirhorizontalandverticalsize policies
settoFixed.Theywon’tberesized.Wewantallthe other
widgets to stretch when we resize the dialog. The Status
label has its horizontal and vertical size policies set to
Preferred. The two list widgets, listWidgetRepo and
listWidgetStatus, have their horizontal and vertical size
policies set to Minimum Expanding.
Select Form > Preview from the menu and make sure
that the dialog resizes properly when you stretch it by
pulling on the bottom right-hand corner of the dialog.
There are just a couple more steps. Select Edit > Edit
Buddies from the menu and make the Git Repositories
label a buddy of listWidgetRepo. Then make the Status
label a buddy of listWidgetStatus.
Now, select Edit > Edit Tab Order from the menu
and set the tab order to 1. listWidgetRepo,


  1. listWidgetStatus, 3. pushButtonRefresh and

  2. pushButtonClose.
    Finally, select Edit > Edit Signals/Slots and drag a
    line between pushButtonClose and the Close Button on
    the top right-hand corner of the gitStatus Dialog. Use
    the Configure Connection dialog to make the SIGNAL
    clicked(bool) connect to the SLOT accept(). This is


o youneeda handkeepingallyourgit
repositoriesuptodate?Doyouwanta visual
reminderofthemodified,added,renamed,
untrackedanddeletedfilesinyourgitworking
directories?Builda Qt5 Python3 GUIapplicationthat
findsallofyourgitrepositoriesanddisplaystheirstatus
throughcolours.
TheleftpanelofFigure1 (seeright) showsthe
locationsofallofthegitworkingdirectoriessorted
alphabetically.Wehaveselectedthegitworking
directoryhome/js/Development/historyDialog.
Noticethatitscolourisred,whichindicatesthe
presenceofmodifiedfilesintheworkingdirectory.
Therearealsodeletedanduntrackedfiles,butthe
overallstatusisdeterminedbythepresenceofmodified
files.Therightpanelshowstheissuesthatgitreports
aboutindividualfilesinthegitworkingdirectory.They
arecolour-codedbyimportance,whereredsignifiesan
importantissue,orangesignifiesa less-importantissue
andgreensignifiesthatgitreportednoissues.
OurhistoryDialogdirectoryshowninFigure1 isnot
currentlyused.It couldbedeleted,butwecanalsotell
gitStatus.pytoignorethisrepositorybyaddingit tothe
gitStatus.inifileinthesamedirectory.
You’llbuildthedialog-basedapplicationvisually
usingQt5 Designerandthensaveit as
GitStatusDialog.ui. ThisisanXMLfilethatcontains
information about all of the widgets in the application
and their properties and configurations. You’re going to
use a tool to convert it into a Python script.
Execute Qt 5 Designer from the system menu or
from the command line. In order to run it from the
command line type: designer-qt5.
You should see the File > New Dialog as shown in
Figure 2 (see opposite). You need to add the methods
shown in the boxout (see opposite). Build the dialog
from scratch by clicking on Dialog Without Buttons
under Templates/Forms. You can also, of course, start
with your copy of GitStatusDialog.ui.
If you’re building it from scratch, you will want to add
widgets from the palette found on the left; add three
QLabels, two QTextEdits, two QListWidgets and two
QButtons and position them as shown in Figure 3 (see
page 90). The upper-right corner of the screen in Figure
3 shows the added widgets and their assigned names.
The QLabels and the QTextEdit widgets should have
a focusPolicy of NoFocus selected. Set the properties of
these in the property editor, seen on the right-hand side

Figure1: ThegitStatusapplicationhasfoundthegitrepositories. The
StatuspaneshowsissuesreportedbygitforthehistoryDialog repository.

Monitor git projects


John Schwartzman discusses a PyQt5 program that will find all of


yourgitrepositories and display their status.


John
Schwartzman
is a long-time
engineering
consultant to
business and
government. He
also teaches
Computer
Science at a
local college.
John can be
reached at john@
fortesystem.
com.

D


PYTHON


OUR
EXPERT

Runningpydoc
./gitStatus.py
providesa list
ofthemethods
anddatain
gitStatus.py
andallows
youtobrowse
it usingless.
Runningpydoc
./gitStatus.py
> gitStatus.txt
willproduce
gitStatus.txt
youcanbrowse
inyoureditor.

CODING ACADEMY

Free download pdf