Libdiaqt Library

This library contains several useful classes and a collection of utility functions for Qt programming. The include files for the classes are named by the class name all in lower case, with extension .h, except for dialog_frame.h for DialogFrame. The include file for the utility functions is dia_qtutils.h.

Derived Qt Classes


ArrowButton::ArrowButton( Qt::ArrowType type, QWidget * parent, const char * name)
ColorSelector::ColorSelector(QWidget *parent, QString label, int red, int green, int blue, int hotFlag, int hotKey, bool rounded, const char *name, Qt::WindowFlags fl)
DialogFrame::DialogFrame(QWidget *parent, int numButtons, const char *labels[], const char *tips[], bool equalSized, const char *caption, const char *fallback, const char *name, Qt::WindowFlags fl)
DialogFrame::DialogFrame(QWidget *parent, int numButtons, int numRows, const char *labels[], const char *tips[], bool equalSized, bool rounded, const char *caption, const char *fallback, const char *name, Qt::WindowFlags fl)
void DialogFrame::changeEvent(QEvent *e)
MultiSlider::MultiSlider(QWidget *parent, int numSliders, const char *titles[], int minVal, int maxVal, int decimals, bool horizontal)
void MultiSlider::setDecimals(int slider, int decimals)
void MultiSlider::setScaling(int slider, float scaling)
void MultiSlider::setValue(int slider, int value)
void MultiSlider::setRange(int slider, int minVal, int maxVal)
void MultiSlider::setMinMaxVal(int slider, int minVal, int maxVal, int value)
QSlider *MultiSlider::getSlider(int slider)
void MultiSlider::setEnabled(int slider, bool enabled)
void MultiSlider::showWidgets(int slider, bool show)
ToolEdit::ToolEdit( QWidget * parent, int columns, const char * name)
void ToolEdit::setColumnWidth(int columns)

Functions in dia_qtutils.cpp


QPushButton *diaPushButton(const QString &text, QWidget *parent, QBoxLayout *layout)
QCheckBox *diaCheckBox(const QString &text, QWidget *parent, QBoxLayout *layout)
QRadioButton *diaRadioButton(const QString &label, QWidget *parent, QButtonGroup *group, QBoxLayout *layout, int id, const QString &tooltip)
QLabel *diaLabel(const QString &text, QWidget *parent, QBoxLayout *layout)
QAbstractSpinBox *diaLabeledSpin(int nDecimal, float minValue, float maxValue, float step, const QString &text, QWidget *parent, QBoxLayout *layout, QLabel **labelPtr, float heightFac)
QVBoxLayout *diaVBoxLayout(QBoxLayout *layout)
QHBoxLayout *diaHBoxLayout(QBoxLayout *layout)
QSlider *diaSlider(int min, int max, int step, int value, QWidget *parent, QBoxLayout *layout)
QFrame *diaFrameLine(QWidget *parent, QBoxLayout *layout)
void diaSetChecked(QAbstractButton *button, bool state)
void diaSetSlider(QSlider *slider, int value)
void diaSetSpinBox(QSpinBox *box, int value)
void diaSetDoubleSpinBox(QDoubleSpinBox *box, double value)
void diaSetSpinMMVal(QSpinBox *box, int min, int max, int value)
void diaSetGroup(QButtonGroup *group, int id)
void diaSetEditText(QLineEdit *edit, const QString &text)
void diaShowWidget(QWidget *widget, bool state)
int diaGetButtonWidth(QWidget *widget, bool rounded, float factor, const QString &text)
int diaSetButtonWidth(QPushButton *button, bool rounded, float factor, const QString &text)
void diaScreenSize(int &width, int &height, QWidget *widget)
void diaMaximumWindowSize(int &width, int &height, QWidget *widget)
void diaMinimumWindowPos(int &left, int &top, QWidget *widget)
void diaLimitWindowSize(int &width, int &height, QWidget *widget)
void diaLimitWindowPos(int neww, int newh, int &newdx, int &newdy, QWidget *widget)
void diaLimitEditStretch(QLineEdit *edit, QBoxLayout *layout, int numDigits, int stretch)
void diaSetTitle(const char *title)
int dia_puts(const char *message)
int dia_err(const char *message)
int dia_ask(const char *question)
int dia_ask_forever(const char *question)
int dia_choice(const char *question, const char *lab1, const char *lab2, const char *lab3)
int diaQInput(int *value, int low, int high, int decimal, const char *prompt)
QString diaOpenFileName(QWidget *parent, const char *caption, int numFilters, const char *filters[])
void dia_vasmsg(const char *msg, ...)
void dia_smsg(const char **msg)
void diaHandleFontAndDPI(bool handleStyle)
float diaGetAppDevPixRatioInfo(float &maxDPR, int &devPixVaries)
float diaAdjustFontForDPI(float devPixRatio, bool debug)
void diaSetQtLibraryPath(void)
void diaQtNoInitOMPonFork()

ArrowButton class

ArrowButton::ArrowButton ( Qt::ArrowType type, QWidget * parent, const char * name)

A toolbutton with an arrow icon that is nicer than the Qt arrow button.  type should be one of Qt::UpArrow, Qt::DownArrow, Qt::LeftArrow, Qt::RightArrow.  name defaults to 0 and is now irrelevant.  To use this class, you must have the four arrow.png files in a directory named images under the top directory of your project, and they must be included in a resource file, as in 3dmod and midas.

ColorSelector class

ColorSelector::ColorSelector(QWidget *parent, QString label, int red, int green, int blue, int hotFlag, int hotKey, bool rounded, const char *name, Qt::WindowFlags fl)

This class provides a color selector with a sample color panel, and three sliders for adjusting red, green, and blue.  label is used to set a label at the top of the panel, and the color is initialized with red, green, and blue.  It manages the color of the panel continuously during changes, and emits a signal for a new color if the slider is clicked.  It will also emit signals during a drag if hotFlag is not 2; if the key given by hotKey is up when hotFlag is 0; or if that key is down when hotFlag is 1.  name defaults to NULL, and the window flags default to Qt::WDestructiveClose | Qt::WType_TopLevel.
     Signals emitted are:
void newColor(int r, int g, int b);  -  When the color changes
void done();   -  When the Done button is pressed
void closing();  -  When the window is closing
void keyPress(QKeyEvent *e);   -  When a key is pressed
void keyRelease(QKeyEvent *e);  -  When a key is released
     In addition, there is one method:
bool hotSliding();  -  Returns true is a slider is being dragged

DialogFrame class

DialogFrame::DialogFrame(QWidget *parent, int numButtons, const char *labels[], const char *tips[], bool equalSized, const char *caption, const char *fallback, const char *name, Qt::WindowFlags fl)

DialogFrame provides a widget whose default style is to be a dialog box that destroys itself on closing.  Its main area is a QVBoxLayout, protected member mLayout, that can be populated with widgets by the inheriting class.  The bottom row(s) will have numButton buttons, with text given in labels.  Tooltips for each button can be provided in tips if it is non-NULL.  The buttons will be equally sized if equalSized is true; otherwise they will all be just big enough for their respective text. Set numButtons to 0 to have no buttons.  The window title will be set to caption, or to fallback if caption is NULL.  name defaults to 0, fl defaults to Qt::Window.
The class emits two signals: actionPressed(int which) and actionClicked(int which) with the argument providing the number of the button pressed or clicked.

DialogFrame::DialogFrame(QWidget *parent, int numButtons, int numRows, const char *labels[], const char *tips[], bool equalSized, bool rounded, const char *caption, const char *fallback, const char *name, Qt::WindowFlags fl)

This alternate constructor includes numRows to specify the number of rows, and rounded to indicate that the style has rounded buttons.  Other items are as above.

void DialogFrame::changeEvent(QEvent *e)

A virtual protected function that maintains the size of the buttons upon font change.  If the inheriting class reimplements this, it should first update the mRoundedStyle variable, then call DialogFrame::changeEvent(e), then set its font-dependent sizes

MultiSlider class

MultiSlider::MultiSlider(QWidget *parent, int numSliders, const char *titles[], int minVal, int maxVal, int decimals, bool horizontal)

This class provides an arbitrary number of horizontal sliders, each with a label and an integer numeric output that is managed as the slider is dragged.  The number of sliders is set in numSliders, their text labels in titles.  Overall minimal and maximum values and number of decimal places can be set with minVal (default 0), maxVal (default 255), and decimals (default 0).  The page step and single step size of each slider is set to 1.  Set horizontal flag to true to have the sliders arranged in a QHBoxLayout instead of a QVBoxLayout.  The layout can be obtained with:
   QBoxLayout *getLayout();
As a slider value changes, the class emits a signal:
   void sliderChanged(int slider, int value, bool dragging);
with slider equal to the slider number, value with the new integer value, and dragging true if the slider is being dragged.

void MultiSlider::setDecimals(int slider, int decimals)

Sets the number of decimals to display for the given slider number to decimals.

void MultiSlider::setScaling(int slider, float scaling)

Sets a scaling to divide integer slider values by to get the number to display. When no scaling is set and decimals are specified, slider values are divided by 10 to the number of decimals, and single steps correspond to single digit changes. Scaling can be used to make single steps produce intermediate changes like 0.002.

void MultiSlider::setValue(int slider, int value)

Sets the value of the given slider to value with signals blocked.

void MultiSlider::setRange(int slider, int minVal, int maxVal)

Sets the range of the given slider to minVal, maxVal with signals blocked.

void MultiSlider::setMinMaxVal(int slider, int minVal, int maxVal, int value)

Sets the range of the given slider to minVal, maxVal and the value to value with signals blocked, thus avoiding any signals from an existing value being outside of a new range.

QSlider *MultiSlider::getSlider(int slider)

Returns a pointer to the given slider, or NULL if the value is out of range.

void MultiSlider::setEnabled(int slider, bool enabled)

Sets the enable state of given slider and its associated text fields to enabled.

void MultiSlider::showWidgets(int slider, bool show)

Shows or hides a given slider and its associated text fields depending on the value of show.

ToolEdit class

ToolEdit::ToolEdit( QWidget * parent, int columns, const char * name)

A line edit widget that sends a signal, focusLost(), when it loses input focus, and that can be set to fixed column width by providing the number of characters in columns.  columns defaults to 0.  name is ignored. Use the signal editingFinished() instead of focusLost() and returnPressed().

void ToolEdit::setColumnWidth(int columns)

Set the edit box to fit the number of characters in columns, or resize it to fit a previously specified number if columns is zero

Functions in dia_qtutils.cpp

QPushButton *diaPushButton(const QString &text, QWidget *parent, QBoxLayout *layout)

Makes a new push button with the given text, adds it to layout if it is non-NULL, and sets it for no focus.  layout can be a QHBoxLayout or a QVBoxLayout.

QCheckBox *diaCheckBox(const QString &text, QWidget *parent, QBoxLayout *layout)

Makes a new check box with the given text, adds it to layout if it is non-NULL, and sets it for no focus.  layout can be a QHBoxLayout or a QVBoxLayout.

QRadioButton *diaRadioButton(const QString &label, QWidget *parent, QButtonGroup *group, QBoxLayout *layout, int id, const QString &tooltip)

Makes a new radio button with the given label and with the given parent (which can be NULL), adds it to the button group group with button ID id if group is non-NULL, adds it to layout if it is non-NULL, sets it for no focus, and sets tooltip as the tooltip if it is non-NULL.  layout can be a QHBoxLayout or a QVBoxLayout.

QLabel *diaLabel(const QString &text, QWidget *parent, QBoxLayout *layout)

Makes a new label with the given text and adds it to layout if it is non-NULL. layout can be a QHBoxLayout or a QVBoxLayout.

QAbstractSpinBox *diaLabeledSpin(int nDecimal, float minValue, float maxValue, float step, const QString &text, QWidget *parent, QBoxLayout *layout, QLabel **labelPtr, float heightFac)

Makes a labeled spin box, with the label given by text to the left of the box and right aligned to it, provided that layout is a horizontal layout box in which to place them.  (In a toolbar, layout can be NULL.) minValue, maxValue, and step are the  minimum, maximum, and step sizes for the spin box.  If nDecimal is non-zero, it creates and returns a QDoubleSpinBox with that number of decimal places.  It skips the label if text is NULL.  The focus policy is set to ClickFocus.  Keyboard tracking is turned off.  If a pointer is supplied in the optional argument labelPtr (which is NULL by default), it is returned with the label pointer.  If the optional argument heightFac is supplied, the minimum height of the spin box will be set to that factor times the font height.

QVBoxLayout *diaVBoxLayout(QBoxLayout *layout)

Creates a QVBoxLayout and adds it to layout, replacing the very useful Qt 3 constructor.

QHBoxLayout *diaHBoxLayout(QBoxLayout *layout)

Creates a QHBoxLayout and adds it to layout

QSlider *diaSlider(int min, int max, int step, int value, QWidget *parent, QBoxLayout *layout)

Creates and returns a new horizontal slider with range from min to max, value value and single step size step, adds it to layout if it is not NULL, and sets it for no focus.  The page step and single step are both set to 1.

QFrame *diaFrameLine(QWidget *parent, QBoxLayout *layout)

Creates a QFrame that is a horizontal line with the given parent, and adds it to layout if that is not NULL.

void diaSetChecked(QAbstractButton *button, bool state)

Sets a checkbox or checkable toolbutton button to state with signals blocked

void diaSetSlider(QSlider *slider, int value)

Sets slider to value with signals blocked

void diaSetSpinBox(QSpinBox *box, int value)

Sets a spin box box to value with signals blocked

void diaSetDoubleSpinBox(QDoubleSpinBox *box, double value)

Sets a double spin box box to value with signals blocked

void diaSetSpinMMVal(QSpinBox *box, int min, int max, int value)

Sets a spin box box to value and sets its minimum and maximum to min and max, with signals blocked

void diaSetGroup(QButtonGroup *group, int id)

Turns on button with ID id in button group group, with signals blocked. The ID must be explicitly assigned such as when adding the button to the group.

void diaSetEditText(QLineEdit *edit, const QString &text)

Sets a text edit edit to the given tex with signals blocked

void diaShowWidget(QWidget *widget, bool state)

Shows or hides widget depending on value of state.

int diaGetButtonWidth(QWidget *widget, bool rounded, float factor, const QString &text)

Determines a button width appropriate for the given text, multiplying by factor and adding height if rounded is true.

int diaSetButtonWidth(QPushButton *button, bool rounded, float factor, const QString &text)

Sets button to a fixed width of appropriate for the given text, multiplying by factor and adding height if rounded is true.  The width is returned to use in setting other buttons to the same width.

void diaScreenSize(int &width, int &height, QWidget *widget)

Returns the desktop size using QApplication::desktop(), or the size of the screen that the widget is on if the optional widget is non-NULL

void diaMaximumWindowSize(int &width, int &height, QWidget *widget)

Gets the maximum window size in width and height, i.e., the desktop size minus assumed borders.  If the optional widget is non-NULL, it uses the size of the screen that the widget is on.

void diaMinimumWindowPos(int &left, int &top, QWidget *widget)

Returns the upper left of the assumed usable area of the screen that widget is in in left and top

void diaLimitWindowSize(int &width, int &height, QWidget *widget)

Limits width and height to the maximum window size; if the optional widget is non-NULL, it uses the size of the screen that the widget is on.

void diaLimitWindowPos(int neww, int newh, int &newdx, int &newdy, QWidget *widget)

Limits the position of a window with width neww, height newh, and desired position newdx, newdy, adjusting newdx and newdy so that the window should be fully on the screen.  This is done differently for X11, Mac, and Windows.  The position will be constrained to the screen where the middle of the given position lies, or to the screen that the window is on if optional widget is non-NULL.

void diaLimitEditStretch(QLineEdit *edit, QBoxLayout *layout, int numDigits, int stretch)

Keeps the line edit box edit from making the dialog wider than needed by making its size hint have no effect, setting a minimum width to hold at least numDigits, and giving a stretch factor of stretch in the layout (default 10) so that it does expand in preference to other items in the layout.

void diaSetTitle(const char *title)

Sets the application title into the static variable Dia_title

int dia_puts(const char *message)

Puts up an application-model message box with the information string in message and an OK button

int dia_err(const char *message)

Puts up an application-modal message box with an error string in message message and an OK button

int dia_ask(const char *question)

Puts up an application-modal message box with the text in question and Yes and No buttons.  Returns 0 for no, 1 for yes.

int dia_ask_forever(const char *question)

Puts up an application-modal message box with the text in question and Yes, Yes Always, and No buttons.  Returns 0 for No, 1 for Yes, 2 for Yes Always.

int dia_choice(const char *question, const char *lab1, const char *lab2, const char *lab3)

Puts up an application-modal message box with the text in question and with up to three buttons, whose labels are in lab1, lab2, and lab3. Supply a NULL to omit a button.  Returns the number of the button pressed, numbered from 1.

int diaQInput(int *value, int low, int high, int decimal, const char *prompt)

Uses QInputDialog to get an integer or float value from the user.  The text should be in prompt; value provides a default or initial value and the new value is returned into this variable unless the user cancels.  The If decimal is 0 it sets up a spin box with low and high as its limits; otherwise it gets a float with the given number of decimal places, with low and high specifying scaled lower an upper limits. Returns 0 if the user cancels.

QString diaOpenFileName(QWidget *parent, const char *caption, int numFilters, const char *filters[])

Gets the name of a single existing file with a file chooser that will show caption in its title bar.  A set of numFilters filters can be given in filters; the first will be the default filter.  Returns an empty string if the user cancels.

void dia_vasmsg(const char *msg, ...)

Makes a scrolled text window with the text taken a set of character strings passed as variable arguments

void dia_smsg(const char **msg)

Makes a scrolled text window with the text taken from the array of character strings in msg

void diaHandleFontAndDPI(bool handleStyle)

Deals with several QT-version and/or OS-dependent issues that must be done prior to instantiating the QApplication: substitutes a font on Max; enables high-DPI scaling with Qt 5.6+, either by setting the AA_EnableHighDpiScaling attribute or by setting QT_SCALE_FACTOR environment variable on Linux; and sets style on Linux or Mac if handleStyle is true.

float diaGetAppDevPixRatioInfo(float &maxDPR, int &devPixVaries)

Returns device/pixel ratio for the application if Qt version 5.6+, as well as the maximum ratio for all screens in maxDPR and sets devPixVaries to 1 if the ratio varies, 0 otherwise.

float diaAdjustFontForDPI(float devPixRatio, bool debug)

Scales up the size of the application font for Windows by the square root of the device/pixel ratio in devPixRatio, or scales it down by the device/pixel ratio for Linux if QT_SCALE_FACTOR is set, prints the original and scaled font sizes if debug is non-zero, and returns the scaling factor applied

void diaSetQtLibraryPath(void)

Sets up the library path for Qt so that it loads plugins from the IMOD installation. For Qt 5, this needs to be called before starting the QApplication so that platform plugins can be found.

void diaQtNoInitOMPonFork()

Works around a hang when starting a QProcess with version 2017 or 2018 of the Intel OpenMP library for Linux by setting environment variable KMP_INIT_AT_FORK=FALSE. This is called by diaSetQtLibraryPath but may need to be called explicitly if that function is called after forking.