How to hide/show all rows?(QTablewidget)
-
For now i do it manually old way through the for loop with hideRow(i)/showRow(i), but when i have like 50-100k of rows it might took lots of time, sometime up to 30sec+.
Recently, i've used same way but for row removing, with removeRow(i), it also take alot of time, until i found tabWid->model()->removeRows(0, tabWid->rowCount()); That single function removes any amount of rows immediately, no matter how much rows i had.Well now i tried to find same but for hiding/unhiding, and seems no such thing, but i hope i wrong, and there is some way to do that, because 30 seconds is no good.
-
Hi
ui->tableWidget->clear(); also removes all rows.Can i ask why you need to hide such huge amount of rows?
-
If you want to see only some of the rows or none you can implement QSortFilterProxyModel and filter data.
-
@mrjj said in How to hide/show all rows?(QTablewidget):
ui->tableWidget->clear(); also removes all rows.
Knew that, but thanks anyway.
@mrjj said in How to hide/show all rows?(QTablewidget):
Can i ask why you need to hide such huge amount of rows?
Hide all and then show only those which corresponds with value in it(showing like 20 of that rows is fast and easy, but hiding rest of 49980 individually is damn long time).
-
@Engelard
Ok for such use case, you really want to do what @Slawomir_Piernikowski suggests and use
QSortFilterProxyModel and show only the rows that matches some criteria. -
@Slawomir_Piernikowski said in How to hide/show all rows?(QTablewidget):
you can implement QSortFilterProxyModel
I read manual in documentation, it assume that i should use function setModel(); to my tabWidget, but this function is private, and since my tabWidget is made in ui(Designer), i can't remake whole app, create new class just for inheritance, to much code should be repaired.
Is there any other way to do something else, or at least access that private function?
P.S. why this function even private... Such an obstacle for me now.
-
@Engelard
Hi
its private as QTablewidget uses a inner model.
The Widgets version is just a wrapper for the View version.
You could use
QTableView with a QStandardItemModel instead.
Code is also most the same, except you will add the Items to the model and not
directly to Table. -
@Engelard
There is sample here
https://stackoverflow.com/questions/18964377/using-qtableview-with-a-model
THey use a custom model
class VehicleModel : public QAbstractTableModelYou can just use
http://doc.qt.io/qt-5/qstandarditemmodel.html
It looks worse than it is.Basically you can do ( to add a row of data to the model)
QList<QStandardItem *> items; items.append(new QStandardItem("Column 1 Text")); items.append(new QStandardItem("Column 2 Text")); QStandardItemModel* model = new QStandardItemModel(this); model->setColumnCount(2); model->appendRow(items);
and set model to TableView.
This is the same as when you addItem to the TableWidget.They then use setFilterFixedString ( on the QSortFilterProxyModel ) to filter the model.
-
@Engelard
It is always like this. Some widgets has some limitaions like for example private member to which there is no access without inheritens. In your case if you have such a problem it would be better to try to know QTableView and how works Model-View-Controlle.
There is plenty articles but the best is Qt help documentation.
Go to qt help and write in the search line: Model. next choose "Model/View Tutorial" -
@mrjj said in How to hide/show all rows?(QTablewidget):
and set model to TableView.
told u i can't set it because it's private)
Correct answer from Slawomir_Piernikowski , but will do so next time. Good lesson that was, every time when will create some huge/important widget - will do that only through manual creation of additional class with inheritance. No fast creation by hands with Designer anymore.
-
@Engelard
"set model to TableView" -> TableVIEW allows setModel :)
You are using the Widget version. which dont allow it.- No fast creation by hands with Designer anymore.
It has nothing to do with Designer. :)
TableWidget is simply slow with that many items and your method of hiding rows
You can insert a TableView in designer too.So its not about using Designer or not.
You can even use designer with custom classes too.
So even if you decide you need a custom class. There is nothing stopping you for using
Designer for some of the forms and then insert classes from code also.So it was more that the class used could not fulfill the requirements of 100K rows and show hide them. :)
And yes, that came around as it was fast and easy to use TableWidget. and its meant to
be easy to use. However, ease of use comes with a price in this case. -
@Engelard
Do not be disappointed. All widgets are only classes and you use them to create objects.
So it is only about object orientated programming. You have to get to know methods in classes and how to use them. But it takes some time.......but after you once write your soft it will be easier to use Model view control in your other project and then it will not take so much time. -
@mrjj said in How to hide/show all rows?(QTablewidget):
"set model to TableView" -> TableVIEW allows setModel :)
To much tables on this week, did'nt see exactly what you wrote) I Will try it.
UPDATE.
Read about tableView. There is so much to remake, replaced my tabWid with View, about 200 errors got, will take a day to remake whole app)) Or maybe more.
QTableView (and other Model/View widgets) is preferable for displaying a significant amount of data.
Well, did'nt know that when i started my app)
-
@Engelard
well that 200 errors is mostly about same stuff i assume.
But yeah, switching to View can be a bit involving later.Btw, you can try
ui->tableWidget->setUpdatesEnabled(false);
for loop with hiderow
ui->tableWidget->setUpdatesEnabled(true);
and see if that speed it up.
But changing to View version will make you more happy in the long run :) -
@mrjj said in How to hide/show all rows?(QTablewidget):
Btw, you can try
ui->tableWidget->setUpdatesEnabled(false);
for loop with hiderow
ui->tableWidget->setUpdatesEnabled(true);
and see if that speed it up.Will not help here - QHeaderView::setSectionHidden() doesn't care for such things and this is simply not meant to hide more than a few rows.
-
@mrjj said in How to hide/show all rows?(QTablewidget):
TableWidget is simply slow with that many items and your method of hiding rows
Why then TableWidget exist? Why they dont just left only View type, what can do tableWidget what can't View?
@mrjj said in How to hide/show all rows?(QTablewidget):
You can even use designer with custom classes too.
How. In my test program i created tableView in designer, how should i create new class for it? I rly can't get it, how it might be created from ui and my custom class simultaneously.
-
@Engelard
Hi
It exists for use cases with just a few hundred items and
is meant as a convenient class. ( it is a View with private model)- how it might be created from ui and my custom class simultaneously.
You can use a feature of Creator called promotion.
http://doc.qt.io/qt-5/designer-using-custom-widgets.html
Think of it as runtime replace. You place say a normal Widget on the form.
and then promote it to your widget subclass. Then when run, its your class
instead of the default one.
- how it might be created from ui and my custom class simultaneously.
-
@Christian-Ehrlicher
Ah, i though HideRow was something else :)
So treeWidget->setUniformRowHeights(true);
will not speed anything up either i assume. -
@mrjj said in How to hide/show all rows?(QTablewidget):
You can use a feature of Creator called promotion.
Hey nice feature. The only thing i don't like, is this message EVERY time i compile my project(brow.h is my mainWindow, main class if you wish):
And every time clicking yes yes yes...