Adding widgets to QGridLayout at same position in Qt? - c++

I have tested adding widgets(QFrame) to a QGridLayout at the same position & it is working fine. Due to the rigidity of code I cannot add a Stacked Widget. So I am adding the Widgets in the same position & making all (except the one I want to display) hide by setVisible(false);
Is this method fine or is my code running by chance & might crash some day?
Thank You.

Qt's help does not forbid you to do this, though this code is strange. I would definitely recomend you to use QStackedLayout, or, at least, QHBoxLayout.

I am not sure whether this is the good method of doing things.
I got an excellent answer to another question here: Remove Widget from QGridLayout in Qt?
So I guess it is better to remove the widget by using the algorithm as mentioned in the answer to above question.

Related

Qt: List of (custom) QWidgets without performance problems

I'm right now creating an Qt-application and have following problem:
I designed a custom QWidget with some labels and checkboxes. The application should now show a list of the custom QWidgets. I tried the QListWidget but is very slow for my use case. I want to add over 6000 elements of my custom QWidget. If I create these instances of the element and add it to the QListWidget the application will crashed.
Which is the best approach for my issue?
Thanks a lot!
As others have noted, QListWidget or QListView is the way to go. Also note that you should not display custom widgets with it, try using a custom QStyledItemDelegate instead and draw the items yourself. Depending on what you need, this can get complex really fast. I have used QTableView with this approach with tenth of thousands of items without performance problems.
If you really need to display custom widgets, check out a library I wrote some time ago for that exact purpose: longscroll-qt
.

Hide Page/Tab from QTWidget - QT 5.5

i need to hide tabs from a existing project in QT, i don't want to delete the code because i have to set parameters on that code, the Application relay on that too. Seems like QT hasn't built-in hide(); function, i tried to edit stylesheet to make it smaller, but doesn't work too, i've looked on the internet and seems like this is a known issue. Does somebody have some tricks to avoid this?
Only thing i was able to come up with is:
ui->TabObject->setEnabled(false);
basically i disable objects in the tab to make them not usable by the user, but this is not a good thing for the whole UI.
Maybe by calling QTabWidget::removeTab(index) - this removes the tab from the QTabWidget, but does not delete the tab's QWidget.

Add some text to a QToolBar

I am having trouble adding some text in a QToolBar. I can only add Actions. Also I have two actions with their rerspectives icons in my QToolBar but I want to separate them and I canĀ“t either.
My newbie approach was to add empty actions to simulate blank spaces between the icons. But the user can click on the blank spaces.
I am using the Design function of QT Creator. Some help would be really apreciated.
It looks like you can't do it from within Designer:
https://bugreports.qt.io/browse/QTBUG-1267
That's an oooold suggestion, too, with a low priority to boot, so it probably won't get fixed any time soon.
You can get your hands dirty and do it in code, however.

Qt::How to lower the text in a QSpinBox

I'm using a spinbox with a custom font which looks too high in the spinbox. How do I move the text lower?
I have already reimplemented QStyle and made the font lower in another widget but I can't find where to do it with the spinbox. There must be a QRect somewhere where you can just move the top of it but I don't know and can't seem to find where it is.
Qt specifies a QStyle::SC_SpinBoxEditField, which appears to be what you want to modify. If I recall correctly from a few years ago when I was doing stuff with styles, you should be able to hook into getting options for that subcontrol, which would include the rect within which it is supposed to be drawn. Modifying that might get the result you want. If not, it is a place to begin searching for your answer.
This is more of a guess than a positive answer, but you might be able to do this with stylesheets:
spinbox->setStyleSheet("QSpinBox { bottom: -2px;}");
Ideally there would be a subcontrol or something for just the text, but the stylesheet documentation doesn't list one, which might imply the above will have undesirable consequences.
You can do:
spinBox->setAlignment(Qt::AlignCenter);//Or the Align Flag that you want
I hope this help.

Qt - How to do superscripts and subscripts in a QLineEdit?

I need to have the ability to use superscripts asnd subscripts in a QLineEdit in Qt 4.6. I know how to do superscripts and subscripts in a QTextEdit as seen below but I can't figure out how to do them in QLineEdit because the class doesn't contain a mergeCurrentCharFormat() function like QTextEdit does. Please help. Thanks
void MainWindow::superscriptFormat()
{
QTextCharFormat format;
format.setVerticalAlignment(QTextCharFormat::AlignSuperScript);
if(ui->txtEdit->hasFocus())
ui->txtEdit->mergeCurrentCharFormat(format);
}
QLineEdit wasn't really made for this type of thing, as it was designed for simple text entry. You have a few options, however. The simplest one is to do as Hostile Fork suggested and use a QTextEdit, and add a style override to not show the scroll bar (which I assume would remove the arrows). The more complex one would be to either inherit QLineEdit and do your own drawing, or to make your own widget completely that appears similar to the QLineEdits do.