PDA

View Full Version : Dynamic number of rows and columns with QTableWidget



Med
5th March 2006, 17:02
Hi all!

I've a QTableWidget in a Window. When the window is resized by the user, the size of QTableWidget is adapted automatically. As all my QTableWidgetItem have a fixed size, i'd like to change the number of rows and columns of my QTableWidget to display as many elements as possible. Unfortunately i encounter several problems:

1) If i want to populate my QTableWidget in the QMainWindow constructor, tableWidget->width() and tableWidget->height() keep sending me the wrong size. I guess it is because the real size is calculated *after* the constructor. Alas i need to know the actual size from the constructor if i want to set the right number of rows and columns. Any idea?

2) I can't seem to find a kind of "resized" signal which i would send to a function that would change the number of rows/columns of my QTableWidget before repainting it. Without it i can't know when the widget is resized and when i've to change the geometry of my table.

3) Displaying and resizing (changing the number of rows and columns) is very slow as i have to 65536 (and probably more later) items to move.

If you want to get the idea of what i want to do, check gucharmap (the gnome character selector). When you change the size of the window, the grid is automatically adapted. I need exactly this. :)

Thank you very much for any tips,

Med

wysota
5th March 2006, 17:16
1) If i want to populate my QTableWidget in the QMainWindow constructor, tableWidget->width() and tableWidget->height() keep sending me the wrong size. I guess it is because the real size is calculated *after* the constructor. Alas i need to know the actual size from the constructor if i want to set the right number of rows and columns. Any idea?

The size is calculated after the widget is shown. You can't work it around.


2) I can't seem to find a kind of "resized" signal which i would send to a function that would change the number of rows/columns of my QTableWidget before repainting it. Without it i can't know when the widget is resized and when i've to change the geometry of my table.

There is resizeEvent() which you can override (either by an event filter or by subclassing). This is also a solution to your first problem. A resize event is triggered right after the widget is shown (or rather "assigned size").


3) Displaying and resizing (changing the number of rows and columns) is very slow as i have to 65536 (and probably more later) items to move.
You can temporary disable updates of a widget with setUpdatesEnabled(false);

Med
5th March 2006, 17:29
Thanks wysota. I'll see how the events filter works.

Med
5th March 2006, 19:37
There is resizeEvent() which you can override (either by an event filter or by subclassing). This is also a solution to your first problem. A resize event is triggered right after the widget is shown (or rather "assigned size").


I've tried to subclass it, unfortunately when calling tableWidget->height() from resizeEvent(), it still reports the wrong size. :( However, as soon as i resize the window, the reported size is correct. It is a bit frustrating that it doesn't work at the first resizeEvent() call.

wysota
5th March 2006, 19:40
I've tried to subclass it, unfortunately when calling tableWidget->height() from resizeEvent(), it still reports the wrong size. :( However, as soon as i resize the window, the reported size is correct. It is a bit frustrating that it doesn't work at the first resizeEvent() call.

You should use values given by QResizeEvent:


MyWidget::resizeEvent(QResizeEvent *e){
qDebug("New size is: %d x %d", e->size().width(), e->size().height());
QWidget::resizeEvent(e);
}

On the other hand the docs say the widget already has it's new size set...

You could also try showEvent().

Med
5th March 2006, 19:57
On the other hand the docs say the widget already has it's new size set...

You could also try showEvent().

Thank you very much! In fact using resizeEvent(), the size of the window was correctly reported while the size of the tableWidget wasn't. However using showEvent() as you suggested, the widget size was correctly reported. I guess the widget size is actually set after the resizeEvent() and before the showEvent(). Now i can continue toying with Qt. :)

wysota
5th March 2006, 21:05
Maybe you overrode resizeEvent or the wrong widget? :)