Results 1 to 7 of 7

Thread: Dynamic number of rows and columns with QTableWidget

  1. #1
    Join Date
    Mar 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Dynamic number of rows and columns with QTableWidget

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic number of rows and columns with QTableWidget

    Quote Originally Posted by Med
    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);

  3. #3
    Join Date
    Mar 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic number of rows and columns with QTableWidget

    Thanks wysota. I'll see how the events filter works.

  4. #4
    Join Date
    Mar 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic number of rows and columns with QTableWidget

    Quote Originally Posted by wysota
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic number of rows and columns with QTableWidget

    Quote Originally Posted by Med
    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:

    Qt Code:
    1. MyWidget::resizeEvent(QResizeEvent *e){
    2. qDebug("New size is: %d x %d", e->size().width(), e->size().height());
    3. QWidget::resizeEvent(e);
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    You could also try showEvent().
    Last edited by wysota; 5th March 2006 at 19:42.

  6. The following user says thank you to wysota for this useful post:

    Med (5th March 2006)

  7. #6
    Join Date
    Mar 2006
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic number of rows and columns with QTableWidget

    Quote Originally Posted by wysota
    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.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dynamic number of rows and columns with QTableWidget

    Maybe you overrode resizeEvent or the wrong widget?

Similar Threads

  1. QTableWidget: Disable 'snapping' to rows and columns?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 20:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.