Qt: List of (custom) QWidgets without performance problems - c++

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
.

Related

Menu designed for use without mouse. What is the best way to implement?

I'm writing a GUI using QT for embedded system with linux. This device has no mouse. Only display and specific keyboard with keys: up, down, return and 7 number keys.
The software will be used to set some parameters of device and read them (some charts also).
Example of how menu could look:
after OPTION 1 selected
After SUBOPTION 1 selected some table with data is loaded.
Sometimes after selecting option i need to load specific widget and sometimes just another set of options.
I think it is possible to implement custom labels and kind of list widget that aligns them.
I guess it is also possible to use Qt's MVC classes for it. They are highly customizable, but i never made custom views and delegates.
Maybe i just need to create QtListView with stringlist model and apply stylesheet to it so it gets look more like buttons. And based on selection in list load next widget.
Which way is better and why?
Is there any easier ways to accomplish this?
Any ideas would be appreciated.

Implement as custom widget or try to reuse QListWidget?

I'm creating a control that can be used for manual inspection of Tesseract-OCR's output.
Currently I've implemented it as a custom widget, where I do all the drawing (I use a QScrollArea to handle the scrolling at least), and it looks like this:
As you can see, the images are "flowing", like word-wrapped lines in a text editor, and I use the space below the header text, so I don't have 2 clearly separated columns.
Now, it was suggested to me that using a custom widget is 'reinventing the wheel', but the problem is, I don't see a clear way to customize one of the existing container widgets enough to look and behave like this, that would involve significantly less effort or code. Neither is it clear at all to me what is the best way to do it. At most, I can think of using a QListWidget with owner-drawn items and variable item heights.
Am I wrong to use a custom widget here?

how to create ms-access like continuous subforms (widgets) in Qt?

I'm thinking of porting my access application to Qt. I am interested to learn how to do continouos subforms, sub custom widgets for presenting/editing/inserting data from recordset in a verically scrollable non datagrid fashion. Meaning I could put button, label, combo, lineEdit... whatever, for every record.
I like QTableView and delegates. I just don't know if it could be modified to fully emulate access subform.
Sidequestion(maybe the same answer)... how do they DO those continuous forms in access under the hood.
thanks
... not real application data in that example recordset
Qt MVC is probably the best/easiest answer for your question ( http://qt-project.org/doc/qt-4.8/model-view-programming.html ), and with QTableView you should be able to achieve what you want.
Another solution could be: if you have a fix set of column items in every row you could simply design a QWidget with the contents of the row and paste your items (rows) into a QVerticalLayout.
Although I would sugget to try with MVC because that's the preferred way and in this case you could even port it to use QML UI if you want (while you can use the same data classes for the 'backend'). QML is definitely the best approach for (even slightly) animated UIs, and it's mature enough to use it already (it's part of Qt 4.8 and will be the 'star' of Qt 5).

create a scrollbar in a submenu qt?

I have a map application and a submenu which has dynamically added objects (i.e. points on a map) added to the submenu, depending on the layer that is loaded. I have the ability to hide each individual objects (i.e. a point) by clicking on the corresponding submenu item. Is there any way to organize the submenu? When there are a lot of points (i.e. 100) the entire submenu takes up the screen. Can I add a scrollbar to a submenu? I looked in the documentation, but couldn't find anything that support this feature.
From this bug report I was able to find out that you could do the following:
submenu->setStyleSheet("QMenu { menu-scrollable: 1; }");
Works like a charm.
There is no such possibility as far as I know.
Maybe you shouldn't use a sub menu for this but prefer a menu entry that show a Point manager GUI of your own which would have a QListWidget displaying all your points items.
I'm aware that this solution will break a (big?) part of your code but I don't see anything else.
I think you may be able to get the effect you want by creating and using your own QStyle subclass (via QApplication::setStyle()), and overriding the styleHint virtual method to return 1 when the StyleHint parameter passed in is SH_Menu_Scrollable. At least, that works for me when I create large QMenu objects and show them as popup menus.... It may also work for QMenus attached to the menu bar, but I havent tried that.
Whilst it is possible by subclassing the QMenu class to create a custom widget and going from there you're better off looking at a better way to display that information. You'll save yourself time and it'll be much easier for your users to not have to scroll through a big list of items in a small area.

Making a game in Qt regarding GUI windows

I've been wanting to program a simple game with a simple GUI using Qt (Its will be a VERY simple game, nothing fancy). What I've been wondering is, how can I create multiple windows and display them when needed? For an example, a battle screen and an inventory screen. The user should only see one of them, but should be able to access the other one when needed. I was using stacked widget but I'm not sure if that's the proper way. Also, is it better to design the windows in the designer or to code them?
A StackWidget certainly would accomplish what you want to do. The reason why it is not always used for this kind of thing, is that it all the screens are pre-created at the beginning and always exist. This means it takes a little longer to initialize, and you are using more resources than you need at any one time
But as you are saying, if this is a simple game, then I don't see a big problem with it. Just as easily, you could also, create an empty layout and swap the inventory and game panels as needed.
Add my vote to everyone else suggesting to use the designer. It is so much easier to manipulate layouts, actions, and such using the designer then through code.
You can see the Designer manual here
So this is what I would suggest:
Create your "battleScreen.ui" - which is the designer file for your battle screen and everything in it, and then create your "inventory.ui". Both of these could be QWidgets, or QFrames, or whatever makes sense.
Then create your "Game.ui" which will be your QMainWindow.
In your Game main window, you can then add your QStackWidget, and place your inventory, and battle screens in the stack widget.
If you don't know how to do that...
1) drag a QWidget into your form (into the stack widget)
2) select the new QWidget and right-click.
3) Select "Promote to..."
4) Fill out the information to promote the QWidget to your inventory class
Promoted Class Name: The name of your inventory class
Header File: The header file of your inventory class
5) Click add
6) Click Promote.
Hope that helps.
Since I'm not sure what your goals are I can't advise whether or not the stacked widget is appropriate but I think you can accomplish quite a lot using the designer and style sheets. If you need to code some parts of the GUI, you can always drop in a place holder widget and either replace it with coded items or make them children of the place holders.
A general answer for a general question:
Use the Designer to create your windows; hide and show the auxiliary windows as needed.
Use a flow manager class to manage the visibility of a related set of windows.
The stacked widget is useful for managing a button/icon whose appearance changes based on state; the different representations live in the stack.