retranslateUi() clears QLabel text - c++

My qt4-based application dynamically changes the GUI language by calling retranslateUi() function. It works like a charm, but when it processes the QLabel which text changes by the program, it tries to translate original text - the one, specified in Qt Designer, not the one, set by my program.
I understand it happens because retranslateUi() is auto-generated during build process so it couldn't possibly know about my changes, but I'd prefer to skip such QLabels completely. Is there a way to tell Qt to skip certain widgets in retranslateUi()? Alternatively, maybe I can disable content change for QLabel in my program?
I know I can sub-class QLabel and by overriding setText() solve this problem, but I prefer to use standard QLabel to be able to manipulate it using Qt Designer.

As I remember, in Designer you can uncheck on QLabel should it be translated. By default it is. Choose label you don't want to be translated, in property editor scroll to "text" property, expand it and uncheck translate checkbox. Then you should generate ui_*.h file again. After that your label shouldn't be in retranslateUi code

Related

Set CSS both in designer and in code for my custom widget

I have a custom widget subclassed from QPushButton, MyButton. It's implemented in a single .cpp file and I'm using it in Qt Creator in my application's form (I've added a QPushButton then promoted it to MyButton).
As discussed here: Should I really use a single qss file for my application instead of having one for each (UI) class? I wish to generally set its CSS inside its constructor (the general look of the button, the background image, hover behavior etc), but I would also like to be able to set it in Qt Creator (maybe customize the font size/color; generally specific to a particular button).
The issue is, as the setupUi call is issued, my buttons are created, they set their styles in their constructors, but then Qt applies the styles set in Qt Creator, immediately overriding mine.
What can I do to achieve this effect?

Changing application's main window GUI layout to be in code

In my application the main window's GUI is designed in the Qt-Creator designer. I have had some trouble in getting it to look just the way I'd like, but I can when doing the GUI in C++ code.
So, I plan to change the application's main window to be laid out in code.
What should I keep in mind when doing this?
How do I make sure all the menu items and button clicks etc. get migrated, too?
In my Qt experience I almost always write layout in code and here is what I can suggest:
a) Spend some time thinking which Layout to use, personally I tend to use either QGridLayout or nested QHboxLayout and QVBoxLayout which give you lot of flexibility.
b) I normally declare all child widgets as class variables always pointers and I create the real objects in the Main windows constructor.
About not to forget any control I suggest to print the XML of the UI file and draw a line on each control you recreate in the code.
As a good starting point, simply copy-pase the setupUi method from the ui_xxx.h file that uic had generated for you. You can then manually edit the setup code to suit your needs.

How to display superscript in QComboBox item?

I want to display 10-8 in QComboBox item. But it displays "sup" tags.
the easiest way is to use special Unicode characters and use them in translation file (direct usage in code may be problematic):
10⁻⁸
If you don't like use translation file try this code:
ui->comboBox->addItem(QString::fromWCharArray(L"10\x207B\x2078"));
ui->comboBox->addItem(QString::fromWCharArray(L"10⁻⁸"));
On my Qt.5.2.1 (Linux) it works.
Also pasting above string in designer also works.
There is no simple way of making this happen. The best way is to override style of QComboBox using QProxyStyle class. Then you can paint the text of a combobox using QTextDocument or similar.
http://qt-project.org/doc/qt-5.0/qtwidgets/qproxystyle.html
More specifically, QComboBox uses
void QComboBox::paintEvent(QPaintEvent *)
{
QStylePainter painter(this);
....
// draw the icon and text
painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
}
and from there you can find how this is drawn from qtbase/src/widgets/styles/qcommonstyle.cpp. Overrides to draw custom text instead of basic text should be apparent, at least for read-only QComboBox.
If you need to edit things in the line edit of the QComboBox, then you'll need to override that QLineEdit with your own.
QComboBox::itemDelegate() only overrides painting of popup list, as it indicated in the documentation. It does not override display of the of editbox text and thus it cannot be used to completely address your problem.
You cannot do this easily, e.g. with html, because QComboBox does not seem to support rich text like some other QWidget subclasses.
However, you could replace the default delegate to draw as you wish, including this. You would need to set your item delegate for the QComboBox, and use QTextDocument in your paint method.
Here is the corresponding bugreport that was submitted a while ago:
Rich-text in combo box items
Alternatively, you double could check the encoding. It may be just a simple issue about the unicode handling. If both work for you, then it is up to you how to handle it.
Based on the following thread thread, you could try this QString method.

(Qt C++) QPushButton setIcon() not working

I'm working on a project that has 40 checkable push buttons in a group and I want the icon of one button to change depending on a value I define. I'm not creating a new pushbutton, just changing the icon of the current one. So I'm testing it by trying to change one button's icon.
Here is my current code:
QPixmap b1d0(":/textures/blocks/textures/blocks/stone.png");
QIcon ButtonIcon(b1d0);
ui->slot_0->setIcon(ButtonIcon);
ui->slot_0->setIconSize(b1d0.rect().size());
The resource path was copied directly from my resource file so it is correct, I've messed with it like crazy but no change so...
slot_0 is my pushButton.
What did I do wrong? Or better yet, am I even allowed to change the icon of an existing pushButton?
Thanks for your time :)
If you are on Windows using MSVC as your compiler, and want to use Qt's resource system, you will need to add
Q_INIT_RESOURCE(res)
to the beginning of main(), where res is the name of your .qrc file without the ".qrc".
To get a full view of this, look at this gist:
https://gist.github.com/alexreinking/5992821

Creating a tree-view with buttons? in QT

I am trying to make a dialog box like below in QT, the only problem is I have no idea what the widget is called. The bar on the left is like a tree-view widget, but when you click on it, it updates the text on the right. Does anybody happen to know what the widget is called or what widget(s) are required to perform this? I am using QT C++ on Windows.
There is an example with Qt showing you how to do this.
https://doc-snapshots.qt.io/4.8/dialogs-configdialog.html
If you're using Qt Creator as IDE, you can find it under the "Demos and Examples" tab in the Welcome Screen too.
It uses a QListWidget for the selector, and QStackedWidget to control the different pages. Connect the currentItemChanged signal of the list widget to change what page should be shown. Everything you'll need is in configdialog.cpp.
If you realy need to add QPushButton into QListWidget, use setItemWidget, or into ListView use QAbstractItemView::setIndexWidget