Results 1 to 9 of 9

Thread: Instantiate QTableWidget dynamically during runtime

  1. #1
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Instantiate QTableWidget dynamically during runtime

    Hi guys,

    I'm developing an application which carries a matrix and I would like to allow the user to display that matrix on demand. I use a QTableWidget within a QDialog to do that.

    When I launch the application the QDialog is created but cannot be displayed (I don't want to...) until a first matrix is computed. At that time the QTableWidget is not instantiated yet. My idea is to instantiate the table only once the first matrix computed (because then I will know the dimensions). If the matrix or its dimension are further modified, the table will be modified accordingly (but not instantiated again for efficiency purposes).

    Apparently there is a problem with my philosophy, because when I run the application, it suddenly crashes when it executes:

    Qt Code:
    1. this->matrixTable = new QTableWidget(size, size, this);
    To copy to clipboard, switch view to plain text mode 

    And I don't understand why... Could somebody explain me why I can't do that and hopefully also tell me how to proceed then ?

  2. #2
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Instantiate QTableWidget dynamically during runtime

    I think that you want a QTableView to do this.

    Base it on a model and use the dataChanged() or layoutChanged() signals to achieve what you are after.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Instantiate QTableWidget dynamically during runtime

    I guess matrixTable is your original table widget. So there is no need to create a new one. just clear the existing and reuse it.

  4. #4
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Instantiate QTableWidget dynamically during runtime

    I modified my dialog but my program keeps crashing and I don't find the root cause.

    My application is defined as follows:

    there are 3 classes:

    Dummy, Coordinator and MatrixDialog

    Dummy performs computation to generate the matrix I need. This matrix is then sent to the Coordinator which forwards it to the MatrixDialog.

    here is the code of MatrixDialog:

    matrixDialog.h

    Qt Code:
    1. class MatrixDialog : public QDialog {
    2. Q_OBJECT
    3.  
    4. public:
    5. MatrixDialog(QString windowTitle, QWidget *parent = 0);
    6. void updateTable(QStringList& strings, SquareMatrix& matrix);
    7.  
    8.  
    9. private slots:
    10. void ok();
    11.  
    12. private:
    13. QTableWidget *matrixTable;
    14. };
    To copy to clipboard, switch view to plain text mode 

    matrixDialog.cpp

    Qt Code:
    1. #include "matrixDialog.h"
    2.  
    3. MatrixDialog::MatrixDialog(QString windowTitle, QWidget *parent) : QDialog(parent) {
    4. this->matrixTable = new QTableWidget(this);
    5. this->matrixTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    6. this->matrixTable->horizontalHeader()->setStretchLastSection(true);
    7. this->matrixTable->verticalHeader()->setStretchLastSection(true);
    8.  
    9. QHBoxLayout *formatLayout = new QHBoxLayout;
    10. formatLayout->addWidget(mapleLabel);
    11. formatLayout->addWidget(mapleEdit);
    12.  
    13. QPushButton *okButton = new QPushButton("OK");
    14. okButton->setFixedSize(okButton->size());
    15. connect(okButton, SIGNAL(clicked()), this, SLOT(ok()));
    16.  
    17. QVBoxLayout *mainLayout = new QVBoxLayout;
    18. mainLayout->addWidget(matrixTable);
    19. mainLayout->addWidget(okButton);
    20.  
    21. setLayout(mainLayout);
    22.  
    23. setWindowTitle(windowTitle);
    24. }
    25.  
    26. void MatrixDialog::ok() {
    27. this->hide();
    28. }
    29.  
    30. void MatrixDialog::updateTable(QStringList& tickers, SquareMatrix& matrix) {
    31. int dimension = matrix.getDimension();
    32. double value;
    33.  
    34. this->matrixTable->clear();
    35. this->matrixTable->setRowCount(matrix.getRows());
    36. this->matrixTable->setColumnCount(matrix.getColumns());
    37.  
    38. for(int row = 0; row < dimension; ++row) {
    39. QTableWidgetItem *headerItem = new QTableWidgetItem(tickers.at(row));
    40. matrixTable->setHorizontalHeaderItem(row, headerItem);
    41. matrixTable->setVerticalHeaderItem(row, headerItem);
    42. for(int column = 0; column < dimension; ++column) {
    43. matrix.getElement(row, column, value);
    44. QTableWidgetItem *item = new QTableWidgetItem(QString::number(value));
    45. item->setTextAlignment(Qt::AlignCenter);
    46. this->matrixTable->setItem(row, column, item);
    47. }
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    When the Coordinator calls updateTable of MatrixDialog, the application crashes. I even deleted all updateTable's content, but the application still crashes !

    Coordinator is defined as follows:

    Qt Code:
    1. class Coordinator : public QObject {
    2. Q_OBJECT
    3. ...
    4. };
    To copy to clipboard, switch view to plain text mode 


    I'm lost...I just don't understand how I can do an unauthorized access... I would really appreciate some help !

    Thanks guys in advance for your support

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Instantiate QTableWidget dynamically during runtime

    Run a debugger and see where it crashes exactly. I guess its on tickers.at(row) are you sure your string list is as long as your dimension?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Instantiate QTableWidget dynamically during runtime

    Qt Code:
    1. matrixTable->setHorizontalHeaderItem(row, headerItem);
    2. matrixTable->setVerticalHeaderItem(row, headerItem);
    To copy to clipboard, switch view to plain text mode 
    And this is also buggy! if matrix.getRows() > matrix.getColums()

  7. #7
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Instantiate QTableWidget dynamically during runtime

    I believe the issue is somewhere else because if I delete all the instructions within updateTable, the application still crashes. But If the Coordinator doesn't call updateTable, the application runs ok...

    Thank you so much for your help, I really appreciate...

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Instantiate QTableWidget dynamically during runtime

    Then how the Coordinator is calling the member function of MatrixDialog. Have you check the pointer?

  9. The following user says thank you to Lykurg for this useful post:

    ouekah (21st March 2010)

  10. #9
    Join Date
    Feb 2010
    Posts
    61
    Thanks
    18
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Instantiate QTableWidget dynamically during runtime

    I solved the issue finally ! It was not a Qt issue, but a pointer issue ...(shame on me) ^^

Similar Threads

  1. Instantiate objects only having their class name
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 16:22
  2. wanted to re-instantiate two classes from a class
    By salmanmanekia in forum General Programming
    Replies: 2
    Last Post: 22nd August 2008, 08:59
  3. Please instantiate the QApplication object first
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2007, 12:20
  4. QAxContainer: can't instantiate control
    By fabien in forum Qt Programming
    Replies: 0
    Last Post: 24th July 2007, 11:22
  5. Dynamically sized QTableWidget
    By therealjag in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2006, 21:06

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.