Add QSqlQueryModel or index value to QListWidget - c++

Is there a way to add my QSqlQueryModel to a QListWidget? If not is there a way to add the database row id to a QListWidgetItem?
I cant figure this out from the documentation on QListWidget. I can add items to the list but can only think to use the row number of the item as the hidden index which wont work if index numbers are out of order.

OK, here is the deal:
The QListWidget is part of the older Qt3 data widget set.
The widget you actually want is part of the newer "interview" Qt MVC framework. It is called QListView.
It can be a bit confusing.
In general: A "QxxxxView" widget can be bound to a "QxxxxModel" object, such as the QSqlQueryModel you asked about.
See:
http://qt-project.org/doc/qt-4.8/qlistview.html
And:
http://qt-project.org/doc/qt-4.8/model-view-programming.html
Lot's of good examples. For instance:
http://qt-project.org/doc/qt-4.8/sql-querymodel.html
Good luck.
:)

Related

QListWidget sharing the same model

If I understood the concept of the Qt models correct, then I can have multiple views, sharing the same model, so that when data is updated in the model, all views using it, will also update their view accordingly right?
Now I have multiple widgets in my application, which should have individual selections, but they should operate on the same underlying data. So when a row is added in one of the panels, the others should be able to display this new row as well.
Since the QListWidget provides all the features that I need, there would be no point in writing my own model and use it with a QListView. But I realized that QListWidget doesn't allow me to change the model, because the setModel()method is made private.
So is there some way that I can achieve this, without the need of writing a full model on my own? I'm rather new to Qt so maybe there is a ready made general purpose model, that I can use? But so far I haven't found one.
You are trying to use QListWidget to set your own model which is not possible.
You are better off using the MVC pattern the QT imposes. You can refer here on how to use the MVC pattern. This way would be more manageable and correct. Also you can make of the QStandardItemModel or the more general QAbstractListModel or QStringListModel model for lists. You can refer here for more details
But to answer your question yes they is a hack you can use.
Create a QListWidget and treat that widget as your model.
For other views create a QListView and set the model that is returned by the QListWidget
.
For e.g. refer
QAbstractItemModel* model = listWidget->model();
listView->setModel(model);
listView_2->setModel(model);
Then you can use the listWidget as your model. Any operation (add/delete) performed on the listWidget will also affect the listView and listView_2.
I am still of the opinion that instead of the hack creating your own model would be better and more correct.

How to display a selected date and a number in Qt

I'm currently working with the QCalendarWidget and I need some ideas to accomplish the following.
What would be the best way to add the selecteDate from a QCalendarWidget and a number to some sort of table. What I want is basically to have a list of dates with a number attached to each date, these numbers will be added together and the result will be displayed in a QLabel, I also want to be able to delete rows and again update the QLabel every time a row is deleted.
I also want to be able to save the list to an external file.
Should I use a QStringListModel or a QTableView?
How would you accomplish this?
I'm not expecting any code just a general procedure.
Please see the attached image for more details.
Should I use a QStringListModel or a QTableView?
You may want to familiarize yourself with the model/view framework. To put it simply, a model is the actual data that you have and it is independent of how it should be displayed. A view is a particular display implementation of a model. So you could use a model like the QStandarItemModel to store your String+number data and display the model in a QTableView.
Model/View Tutorial from Qt website here
QStandardItemModel class here. Has a simple example inside there.
And, for writing and reading the data to a file, I suggest you could use the QXmlStreamWriter/Reader classes. Refer to Qt xmlWriter/xmlReader

SpinBoxDelegate and QItemDelegate

I work on project It's like SpinBoxDelegate in Qt sample project but I must customize It, I mean having different widget(textbox) in second and third column of table view instead of spainBox.What do you suggest?
Try instead tableView.setItemDelegate(&delegate); from the example something like QTableView::setItemDelegateForColumn().
Plus have a look at this one tutor.
If you can use a QTableWidget instead of a QTableView you can use QTableWidget.setCellWidget() to put another widget inside a cell. For instance, you could put your own QLineEdit or QSpinBox in the cell.
Unfortunately, if you have to display too many of these it will cause performance issues.

Keep the selection after filtering a QTableView with a QSortFilterProxyModel

I created a QTableView linked to a QSortFilterProxyModel linked to another model.
Under the QTableView (in the GUI) there is a QLineEdit used for "searching" an element in the view.
My idea is to write in the QLineEdit what I'm looking for and let the view show only the matched elements. After filtering, I want to select the concerned item and then clean the QLineEdit for returning at the complete view.
Everything works but the selected item that will be filtered will also lose the selection because of the invalidation.
How can I solve this problem?
Why don't you remember the selected rows before the filtering and then just restore it when you're done with filtering.
You could use the QItemSelectionModel directly I'd imagine.
Use QItemSelectionModel::selectedRows() before filtering and select rows after filtering using QItemSelectionModel::select().
I know this thread is very old, but I thought I'd leave the comment for anybody else facing a similar problem.
From what you wrote it looks like the problem is in the QTableView loosing selection when you're cleaning your QLineEdit content. If you're starting your 'search' routine in the line edit's editingFinished() or textChanged() signals you can disconnect from them before changing the QLineEdit and then reconnect back again. Or use a boolean flag and don't change filtering when it's on. It would be much easier to answer your question if you would post up a simplified version of your code with the problem you're having.

QCompleter for QTableWidgetItem to autocompelte

I have a QTableWidget has a QTableWidgetItem that contains a code a user will supply. This code is known to the database and is suppose to autosuggest to the user as he types. How do I connect a QCompleter to a QTableWidgetItem. I know how to do it with a QLineEdit, but I have not found an example with a QTableWidgetItem.
I managed to come up with a solution, though not exactly by using a QTableWidgetItem, basically I placed a QLineEdit in the cell instead and attached the QCompleter to that.
Not sure how far this will get you, but take a look at this
http://docs.huihoo.com/qt/4.4/demos-spreadsheet-main-cpp.html
Search for completer in the code.