Show a dialog with Kdialog (part 1)

If you ever needed to write shell scripts to automatize your tasks, but at the same time wanted a better integration with the running Desktop Environment, an application like Gdialog or Kdialog might be a good choice.

Being a KDE user myself, this article will focus on Kdialog, but I guess the same consideration might still be valid for Gdialog or the ncurses version dialog.

A dialog for everything

The first thing I loved since I moved my first steps into the Linux world, is the powerfull shell installed by default and a large number of command-line applications that allow writing fantastic scripts.

Kdialog is one of them and its goal is to provide a wide range of dialog’s windows, for input and output purposes: a simple message box or a file selection dialog, a mere text input dialog or a selection list. Most of the time these dialogs give substantial help providing a sort of input validation, that’s why shouldn’t be considered decoration only.

Structure of a dialog

A dialog is a window, resizable or not, with a set of elements:

  1. a title to introduce the dialog itself,
  2. a text area intended as a message output or an input request,
  3. a widget area , where the needed components are deployed to perform the request (for input dialogs only),
  4. a set of buttons at the bottom to complete the action.

Later we will see how, reading the standard output and the exit status after a Kdialog call, we will have full control over the choice made.

Let’s start with the output dialogs.

Say something

The simplest use case is a message box with an optional title:

$ kdialog --msgbox "Hello World!" --title "Just saying hello"

kdialog --msgbox

kdialog --msgbox

The icon in the dialog shows that the kind of message is purely informative and doesn’t deserve particular attention, it can be used to communicate the end of a process or the result of a calculation.

The –title switch is optional and can be used for the whole set of dialogs provided by Kdialog.

We can also warn the user:

$ kdialog --sorry "Ehi, I am here!" --title "Warning you"

kdialog --sorry

kdialog --sorry

Or communicate an error:

$ kdialog --error "You made a mistake" --title "Something went wrong"

kdialog --error

kdialog --error

Giving more details

In the latest two cases, we can provide more info within a hidden textbox, which can be displayed pressing the Details button.

The detailedsorry:

$ kdialog --detailedsorry "Ehi, I am here" "Behave or I'll tell your mom!" --title "Warning you"

kdialog --detailedsorry

kdialog --detailedsorry

And the detailederror:

$ kdialog --detailederror "You made a mistake" "Didn't consider the other opportunity" --title "Something went wrong"

kdialog --detailederror

kdialog --detailederror

Notification popup

Another less known output dialog is represented by the Passive Popup which is just a notification message showing above the bottom toolbar which disappears after nth seconds and can show an optional icon:

$ kdialog --passivepopup "I am going to vanish in three seconds" 3 --icon dialog-error --title "Auto-destructive message"

kdialog --passivepopup

kdialog --passivepopup

The icon parameter accepts one of the icon names included in the current theme, it defaults to dialog-information, the same icon used by the msgbox dialog. Later we will see how to get the list without dig into the icon theme folders, but just using Kdialog.

Reading from a file

A useful output dialog is the textbox: it’s intended to show the content of a text file within a resizable window having only a text area and an ok button.

$ kdialog --textbox ~/.bashrc --geometry 640x400+400+300 --title ".bashrc content"

kdialog --textbox

kdialog --textbox

Being the window resizable we can use the optional geometry switch to fix, respectively, the width, height, and the offset from the left and the top border of the screen.

Showing progress

Last but not least the progressbar is the most complex among the output dialogs:

$ kdialog --progressbar "I am progressing" 10 --title "Step by step" --geometry 400x200+400+300

kdialog --progressbar

kdialog --progressbar

Once the dialog is shown, something is printed and the console is left free to evaluate other commands.

To interact with that dialog we need the power of qdbus and that printed code:

$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'value' 5

Setting a progressbar value

Setting a progressbar value

We can also set the autoclose when the value reaches the max

$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'autoClose' 1

Final thoughts

In this first part, I have shown the whole set of Kdialog’s output dialogs, next we will see how to handle the input dialogs and how to use them within our scripts or inside a terminal.


Creative Commons License This work is licensed under a Creative Commons Attribution-NoCommercial-ShareAlike 4.0 International License

Share to

Related articles