Is it possible to add items to a ListWidget during design time. I am using QT Designer however I cannot find any options to add items to a list widget at design time. Any suggestions ?
You need to simply insert a QListWidget in a form when design your GUI in Qt designer. Double clicking on the QListWidget will open another Edit List Widget dialog that will allow you to customize it: insert new items, change properties etc.
Related
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
.
I have a QTabWidget with several tabs, every tab containing a QTableWidget with numbers. Outside of this QTabWidget, I have one button. When the button is pressed, the currently selected widget should be processed. The QTableWidgets are identical in their structure, they just display numbers, and if I have a pointer to one of these widgets, I cannot deduce which tab it came from.
A simple method to identify which widget is currently selected is to call currentIndex(). But this will break when I reorder the tabs in the designer and is probably not working with movable tabs.
Another way is to use currentIndex() and tabText(int index). Like this I have a stable way to find out which tab is currently selected. The disadvantage of this is that I am then dependent on having unique tab texts, and I in general don't like to rely on a UI property to implement functionality.
My solution for now is to give every Widget a different accessible name that I can then read out like this:
QWidget* widget = tabWidget->currentWidget();
QString* name = widget->accessibleName();
This works, but I wonder if there is a better solution, something like the Qt::UserRole that can be assigned to any cell in a QTableWidget.
I'm using Qt Designer to create a Qt 5.4 GUI application (actually, I'm using Qt Creator 3.3.2 but haven't done any C++ yet; everything is in the .ui file).
I'd like to create a box containing an (empty) list of items from which the user could double click inside the box to add a new element to the list (can be single click or other, doesn't really matter).
I just can't figure out how to do this only through the designer. I've checked the properties of the "List Widget (Item-Based)" and "List View (Model-Based)" and there is some options in "editTriggers", but even with the default "DoubleClicked" checked, the compiled application does not allow adding a new element (or deleting...)
Any easy way to achieve this using only the Designer (without doing C++)?
Or is there another widget that would be better suited for this?
Thanks!
If you want to make such a widget, that can be setup completely in the designer only, you are asking a little too much.
If you start with a List Widget and add a bunch of empty named elements, and then make all of them editable, it will seem like you can just double click to add an element.
To make an existing element you've added in the list editable, (in the designer) double click your List Widget, then
in Edit Widget List, click the plus sign, followed by Properties. Scroll down to flags, then check Editable.
This needs to manually be done for each item added.
The C++ to add what you are asking for is very painless.
Add a Push Button. Rename it's objectName to addItemButton.
Then right click on it, and select Go to slot....
Select clicked(), then click Ok.
Now put the following in the slot:
void MainWindow::on_addItemButton_clicked()
{
QListWidgetItem * item = new QListWidgetItem("New Item");
item->setFlags(item->flags() | Qt::ItemIsEditable);
ui->listWidget->addItem(item);
}
Done.
Hope that helps.
I'd like to create a list of QPushButtons that are added and removed at runtime. I figure that an item view widget would accomplish this (QListWidget). The reason for wanting to use an item view instead of a layout is that I would like to scroll through a list of buttons instead of just trying make them all fit. However, I don't see too many examples of QListWidgets being used to store a QPushButtons.
I'd like some tips, pointers, or examples.
I think you should not use QListWidget in this case. Create a widget with a layout and all your buttons. Then put in QScrollArea.
You can add widgets to QListWidget using its setIndexWidget function.
I'm trying to get one cell in a QTableWidget to be a box with button at end"..." with file selector, but don't know how to change what kind of widget the cell is.
To control the types of cells in a table, it's best to use the QTableView class. Then, by using QItemDelegate you can make some table cell a button, to which you may bind a signal that opens a dialog.
I recommend starting by reading about delegate classes in the Qt docs.