How to repaint a widget in QT designer - qt

I am just beginning to learn some programming with QT, and would like to start with the designer portion of QT Creator, before diving into the .cpp, and .h files. I would like to do simple tasks like change the color of a box based on a condition, or perform some arithmetic. But everything I read describes how to the change the code, not do it in the UI editor. Even the QT Designer manual gives no demonstration of how to create a simple calculator in Designer. They only show the code (http://doc.qt.io/qt-5/designer-using-a-ui-file.html). I've loaded the project from the links at the bottom of this page: http://doc.qt.io/qt-5/qtdesigner-calculatorform-example.html, but I'm still stumped as to how it does the arithmetic from looking at the UI editor. I don't see any signals/slots indication an addition on the numbers in the spin boxes. Nor, do I see anything in the properties of the Output QLabel that makes it the sum of Input1 and Input2.
And just to add another level to this simple example, how could I change the color of the output box if a condition is met? Let's just say if the output is larger than 10, make the box blue. I see the palette in the Properties box, but how do I make it conditional, staying within the Designer portion of the Creator application?
Thanks!

You're searching for a way to execute simple code/actions directly from within qt-designer, without using any code behind.
That's not possible, if you use qt-designer and .ui files.
You can connect slots to signals directly from the designer, but you have to implement those slots in your *.cpp file.
If you want to mix the UI design with your application logic take a look at QML.

Related

Is there an elegant way to manage properly a large collection of keyboard shortcuts in qt?

I'm learning qt by working on given examples. I've started to play a bit with keyboard shortcuts. To assign them I've used QtDesigner which is very handy, for example using "return" key to click a research QPushButton.
Here my main class is a Widget called TextFinder, which has a pointer on a Ui::TextFinder class, which is automatically built from QtCreator, which is the standard procedure to encapsulate user interface attributs and methods.
By assigning a shortcut using QtDesigner, the following lines are generated in the Ui::TextFinder class:
#ifndef QT_NO_SHORTCUT
findButton->setShortcut(QApplication::translate("TextFinder", "Return", nullptr));
#endif // QT_NO_SHORTCUT
where findButton is an alias for my QPushButton. So far so good.
Suppose now, in a large program, I want to implement many shortcuts to trigger many kinds of signal and I use QtDesigner to do so, it will generate these lines of code, possibly in different header files corresponding to different widgets. It will become quickly difficult to manage them and have a global vision of the "shortcuts state" of the program.
What would be a good method to manage all the shortcuts in the program at one place? Is it possible to make some config file to perform this task?
Typically in this case you go one level up from the shortcuts and look into Actions.
A QAction is a somewhat global (e.g. on the level of a QMainWindow) vehicle for something that the user can trigger (or toggle) through various ways. To name the most prominent ones,
Menu entries
(Toolbar) buttons
Shortcuts
You can manage your actions through Qt Designer (if you have a QMainWindow), or at a central place in your code. Note that the action encompasses not only the shortcut, but also title, icon etc.
You can arrange a toolbar in Qt Designer by dragging actions onto it, but you can also manually assign an action to any button in code.

Use condition in the Qt Creator designer

I'm pretty new to Qt, and I can't find an answer to very basic question:
is it possible to use conditions when using the ui designer?
for example, that if a var x is set to true widget 1 will be displayed, otherwise widget b will be displayed in the same area?
I know how to do it in the code but wondered if it possible using the design tool...
In my experience there is not a way to do it using the designer in a QWidgets application, though I have to admit I never really tried to do that. In a Qt Quick QML application you can probably use a binding on the visible property to hide/show the desired item.

Qt desktop application

The Qt desktop application I am required to develop has the following GUI:
The application window is split into 3 parts -
left-side window - has a stack of clickable menu items one below the other. Clicking on each item will show up the corresponding UI elements on the prominent right-side window which is much bigger.
right-side window - contains UI to display some data depending on which menu item is clicked on the left-side window. Each left-side window menu item has a different corresponding right-side window UI.
top (header) window - contains some company "brand" related graphics and also a panel to select a serial port from a list along with graphics to represent connect/disconnect status.
How do I develop this kind of UI? What Qt classes should I be using? I'm a beginner to Qt and would deeply appreciate any help. Thank you.
I'd suggest starting in Qt Designer or Qt Creator to create your UI by dragging and dropping different kinds of objects. Qt Designer is a standalone form designer where Qt Creator is full development environment that also includes the functionality of Qt Designer. Using the form designer is a lot easier, particularly when starting out, than creating widgets programmatically.
There's multiple ways to do the list of items on the left. You can use a QListWidget or a series of individual QPushButton instances, one per option. If the list of items changes, then managing the QListWidget content is going to be easier than instantiating a QPushButton for each item, but you might like the appearance of the QPushButton better. It just depends on what you want.
For the right side, look into QStackedWidget. It's specifically designed to display a stack of content where only one item is available at a time.
For the top panel, again, use the form designer to create the layout. You can store an image in a QLabel. A QComboBox might be what you want for the list, or again, you may want a QListWidget. It just depends on what appearance and user experience you're looking for. For the connect/disconnect status, you can use a QLabel and change out the graphics as the status changes.
You'll need to learn about slots and signals; those are crucial to anything Qt-related.

Setting Polygon ROI with mouse in Qt

Anyone have any idea how I can implement this? I'd like to have a function basically exactly like impoly in matlab or the "polygon sections" tool in imageJ, where you click to form a polygonal section and then each node can be adjusted, etc. I'd also like to have access to this function from Qt since I'm trying to make a gui for a small program I wrote.
Also, I'd like to avoid making calls to the matlab function because it's part of the image processing toolbox which isnt free. Thanks.
I think the best way to implement this is using the Qt Graphics View framework. Create a scene with an Item displaying your image in the back and add draggable Items on top representing the corners of your polygon.
Your selection tool should probably be a subclass of QGraphicsObject hosting the polygon corners as child items and a QGraphicsPolygonItem below the corners being updated whenever the user readjusts the selection. As QGraphicsObject inherits QObject, you can emit signals with a QPolygonF or QPolygon argument whenever the selection changes, informing other parts of your application
This demo should be a good example of the corner-adjust functionality you need.
Qt Pathstroke Demo
(uh well, the example implements the drawing and dragging of the control points from scratch.. I'm sure you can do it by using QGraphicsEllipseItem instead and react on their position changes)
I think you would need to code this yourself. There is an excellent example in the C++ GUI Programming with Qt 4 book (there's a PDF copy floating around online; I think it's legal) where they show you how to create a diagram with nodes and links. The chapter is called "Item-based rendering with Graphics View".
The basic idea is that you have some draggable nodes, which are QGraphicsItems with the ItemIsMovable flag set to true, and then some links that connect them, which are QGraphicsLineItems. All of these would go into a composite QGraphicsItem representing the ROI, and all of those would go into a QGraphicsScene, which would be displayed by a QGraphicsView.
Bottom line: there isn't a built-in copy of the MATLAB function, but all the tools are there for you.

how can I have more than a UI for a QMainWindow?

I would like to have a QMainWindow that can change it's look at runtime, i.e when a user clicks a button. Besides keeping references to different UI classes generated by the QtDesigner, is there another way of doing that? Maybe by storing each UI in a layout ? What do you think ?
How much of the main window do you want to change? You could use a QStackWidget any place you want to change things, and change the shown page of the widget when the button is pressed. This will be quick to change, but for large or complicated UIs it may be slightly slower at startup, since it will be creating the widgets for both UIs at the same time. (There are fairly easy ways to change this, also, but they do add complications to something that could be straightforward for most people.) Also, if both layouts should have the same data, just in different places, you have the additional overhead of keeping both sets of UIs up to date while the program is running.
I think I got it now.
You have a QMainWindow and when a certain event is triggered you want to change the appearance of that particular window, like remove some buttons, add a treeview widget or what not.
Well the straight forward approach would be to do it manually, remove some widgets and add new ones using regular C++ code. This can be abit hard if you're used to Qt Designer.
The other way I can think of is using Qt Designer to generate the code for the other appearances and copy it to a special function. The code generated by Qt Designer is usually in a header file called "ui_classname.h" and isn't hard to understand. You will however need to remove some of it as not all would be necessary.
Also, instead of copying the generated code from Qt Designer you could just call it. Usually your widget has a pointer to the generated class and in your widget's constructor you see something like this:
MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MyWindow)
{
m_ui->setupUi(this);
}
and in the corresponding header file:
class MyWindow : public QMainWindow {
...
private:
Ui::MyWindow *m_ui;
};
You could add the additional generated classes for the other appearances and use them when your event triggers.
It might look like this:
class MyWindow : public QMainWindow {
...
private:
void changeAppearance(int id);
Ui::MyWindow *m_ui;
Ui::MyWindowFirstAppearance *m_uiFirst;
Ui::MyWindowSecondAppearance *m_uiSecond;
...
};
void MyWindow::changeAppearance(int id)
{
// some code to remove the current appearance, basically the opposite of what setupUi is doing
if (id == 0)
m_ui->setupUi(this);
else...
m_uiFirst->setupUi(this);
...
}
This has the benefit of using the generated classes directly, so every change you do in Qt Designer doesn't require a change to your main window. The problem is I'm not sure if it's legal to call setupUi more than once and in a place other than your widget's constructor, so you'll have to check that (by looking at what's happening in the setupUi function).
You can dynamically load UI layouts at runtime. Check Qt documentation for QUiLoader class.
This also enables you to upgrade UI layouts without modifying a single line of code.
What I do is to design two UIs under two different QFrames, put the two QFrames in a layout together in the QMainWindow, and then hide() and show() the correct QFrame that you want...
You can use a QTabWidget and hide its buttons. You can then design each GUI in a separate window of the tabWidget. It has the advantage over hiding frames that you won't clutter up your Qt Creator window.
If I'm getting this right, I think you might want to take a loot at style sheets. It allows you to have "skins" for your widgets, much like CSS.
If I didn't get this right, and what you're trying to do is generate .ui files on the fly and create widgets with those using QUiLoader, then you're probably going at this the wrong way since I can't think of a good reason a regular application would need that.

Resources