Results 1 to 3 of 3

Thread: Creating Slot for Setting Number of Rows in QTableWidget

  1. #1
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Creating Slot for Setting Number of Rows in QTableWidget

    I'm using a spinbox to set the number of rows and columns in some QTableWidgets. I did what is described in this thread: http://www.qtcentre.org/threads/2573...a-QTableWidget

    However, I have multiple tables. Can I make a slot which will work for all the tables? The slot function needs access to the specific table it is changing, so I thought about passing a pointer, but the slot needs to have only one parameter, int, in order to work with the signal from the spinbox. So can I "register" my custom slot with the QTableWidget class somehow, so I have access to the table methods in the slot function?

    Thanks in advance.

    Edit: Maybe I should create a customized version (subclass) of QTableWidget?
    Last edited by PeterMW88; 15th April 2014 at 01:02.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating Slot for Setting Number of Rows in QTableWidget

    If I read that correctly, you have only one pair of spin boxes that can be used to set the size of one of several tables.

    What event triggers application of the new row/column count?
    How do you determine which table the dimensions should apply to?

  3. #3
    Join Date
    Apr 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Creating Slot for Setting Number of Rows in QTableWidget

    Hmm... Sorry, it seems I was unclear. I just got it working using a derived class. I've attached a picture which should answer those questions, and copied in the header file which has solved the problem.

    I derived from QTableWidget, hooked up the constructors as required, defined a couple new slots, promoted the tables to my derived version, added the custom slot with the edit button in the designer, and was able to hook up the spin boxes as needed.
    2014-04-14 17_27_34-Matrix Multiplier.jpg
    Header file:
    Qt Code:
    1. #ifndef MATRIXWIDGET_H
    2. #define MATRIXWIDGET_H
    3.  
    4. #include <QTableWidget>
    5.  
    6. class MatrixWidget : public QTableWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit MatrixWidget(QWidget *parent = 0)
    12. : QTableWidget(parent)
    13. { ;}
    14. MatrixWidget(int rows, int columns, QWidget *parent = 0)
    15. : QTableWidget(rows, columns, parent)
    16. { ;}
    17.  
    18. private slots:
    19. void setNumRows(int n)
    20. {
    21. this->setRowCount(n);
    22. }
    23. void setNumColumns(int n)
    24. {
    25. this->setColumnCount(n);
    26. }
    27. };
    28.  
    29. #endif // MATRIXWIDGET_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 17th November 2009, 16:14
  2. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  3. Setting background colors to rows in QTableWidget
    By cnbp173 in forum Qt Programming
    Replies: 0
    Last Post: 24th April 2009, 17:38
  4. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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
  •  
Qt is a trademark of The Qt Company.