How to correctly set the color of a QPushButton? - qt

I can successfully change the color of a QPushButton using setStyleSheet, but because I'm using QT Creator to make the GUI, every time I run qmake and make, the calls to setStyleSheet disappear.
Changing the palette of the button doesn't change its color either.
What's the best way to change to color of the button without having to manually change my ui_window.h file every time I qmake?

Using style sheets is the right way to do, no matter you're using Qt Creator or not.
From what you are describing, it seems you are writing yourself some code into the ui_window.h, which is the wrong way to set the stylesheet.
You can set it in the constructor of your window class, or set it from the GUI editor (Qt Designer) :
double-click on your window.ui in the project tree displayed by Qt Creator.
select your QPushButton in the GUI editor.
locate the styleSheet property in the properties editor.
click on the three dots (...) : this will bring a stylesheet editor.
If you set the stylesheet with the stylesheet editor, nothing will disappear each time you are rebuilding your app.

Related

Code to select the PushButton using Tab Key

I have designed the UI screen which consist a lot of push-buttons and I want to select or can say want to control application using tab key. When I select any particular key, it should change the color of that push-button accordingly.
As far as I know, the focus on using the tab key should work by default and the color of the buttons changes according to focus.
I tried to create a project with several buttons on QMainWIndow and it worked without any additional effort. I'm using Qt 5.15 with KDE on arch linux.
If you want to change the order of the buttons traversal, you can use the button that changes the designer mode in Qt Creator.
And if you want to change colors of the button, you should change the stylesheet of the button with css like code. Here you can see examples of stylesheet customization.

How to hide a widget created by QtDesigner "design" mode programmatically?

I want to know how to create a QLabel in QtCreator Design mode that is hidden by default?In the properties list in the right hand side panel , there's not any option regarding visibility and also adding label->hide() to setupUi function (after compiling ui file and adding to the project headers) makes no difference.
Designer doesn't expose the visible property. You could hand-edit the UI file, but that will be overwritten next time you edit in Designer. It's best to leave it visible in Designer, and just write a line of code after you call setupUi() to hide the widget you don't want to be initially shown.

QDockWidget form in Qt Creator Designer

I'm using Qt Creator 4.5.0 and am trying to create a QDockWidget that I could modify in the builtin designer (I do this all the time with QDialogs and QMainWindows, so this is the first time trying with a QDockWidget). But I'm having no luck with being able to add any widget elements of any kind to the QDockWidget.
Here are the steps I've taken
In the Projects tree on the left, right click on the project and select "Add New..."
In the window that pops up select "Qt" on the left side, then select "Qt Designer Form Class", then select "Choose"
On this page expand the section for "Widgets", then select "QDockWidget", then select "Next"
On this page give the class a name (for me it's "ImageFilesDockWidget"), then click "Next"
On this page select "Finish" to add the files to the project.
From here the "ImageFilesDockWidget.ui" file will automatically show up, so I tried to add some widgets to the view, but nothing would get added. For example, if I clicked and dragged a pushbutton into the center of the dockwidget, then it displayed a red circle with a line through it to indicate I couldn't add the item.
If anyone has run into this problem and knows how to make it work, then that'd be an immense help to me.
Thanks in advance.
update
Currently I'm able to use the designer to customize a standard QWidget object (call it "ImageFilesWidget.ui"). So at the moment my solution is to add a standard QDockWidget to my QMainWindow in the designer, then (still in the designer) I promote the dockWidgetContents from a standard QWidget to my ImageFilesWidget class.
It seems like the problem is when qtcreator 4.5 creates the dockwidgets ui file for you, it doesn't include the "dockWidgetContents" widget that is included in previous versions. Just manually put <widget class="QWidget" name="dockWidgetContents"/> under the "windowTitle" property of the dockwidget and you'll be able to add ui elements to it.

How to repaint a widget in QT designer

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.

Qt creator - make a dynamic interface

I am working on a small UI with QT Creator and its drag-n'-drop tool (QT Designer). Everything was fine till I realized I had to make some widgets dynamic. For example, I have a checkbox and I want some button to appear only if this checkbox is checked by the user or I would like to change the appearance of another widget when the user clicks on some button.
Is it possible to do it only with QT Designer?
Yes, but it's very limited.
Qt designer have signal&slot editor. Yes, you can connect signal clicked(bool) to setVisible(bool) slot on button and make button visible only when checkbox is checked (see screenshot).
But when You need more complex dynamic interface (e.g. creating buttons), designed will not help You.

Resources