Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting data from QTableWidget

Getting data from QTableWidget

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by
    #1

    Hello again,

    I've got a problem getting a pointer to own specified widgets (inherited from QWidget) out of a table cell (QTableWidget):
    main.h:
    @class table_cell : public QWidget
    {
    Q_OBJECT

    public:
    int row, column;
    QString type, cls;
    QHBoxLayout* layout;
    QPushButton* pushbutton;
    QCheckBox* checkbox;
    QTextEdit* textedit;

    table_cell(QWidget* parent, QString p_type, QString p_cls, int p_row, int p_column);
    

    };@

    main.cpp:
    {
    @table = new QTableWidget(rows, columns);
    table_headers_h << "Amount" << "Person 1" << "Person 2" << "Person 3" << "Person 4" << "Person 5";
    table->setHorizontalHeaderLabels(table_headers_h);
    table->setColumnWidth(0, 80);
    for(int i = 0;i < rows; i++)
    {
    table->setRowHeight(i, 26);
    for(int ii = 0; ii < columns; ii++)
    {
    table_cell* cell;
    if(ii == 0)
    cell = new table_cell(this, "textedit", "cell", i, ii);
    else
    cell = new table_cell(this, "checkbox", "cell", i, ii);

        table->setCellWidget(i, ii, cell);
      }
    }
    
    button_calculate = new QPushButton("Calculate");
    connect(button_calculate, SIGNAL(clicked()), this, SLOT(calculate()));
    

    }

    void window_main::calculate()
    {
    for(int i = 0; i < table->rowCount(); i++)
    {
    table_cell* cell_amount = table->cellWidget(i, 0);
    QString amount = cell_amount->textedit->toPlainText();
    if(amount != "")
    {
    QMessageBox *msgbox = new QMessageBox(this);
    QString title = "Betrag";
    msgbox->information(NULL, title, amount, QMessageBox::Ok);
    }
    }
    }@

    The problem is, that I only get a QWidget object with table->cellWidget(int, int), but not my derived class "table_cell", which has the needed member textedit, so the compiler always returns the following error:
    [quote]cell_amount has no member textedit...[/quote]

    So how can I get a pointer to my table_cell objects which are placed into the table cells??

    Greets

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You have to cast cellWidget return value to table_cell using qobject_cast

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on last edited by
        #3

        Hello sGaist,

        first of all thank's for your fast support!
        I read a little bit about dynamic_cast now in my C++ book and yes, it sounds like this is the right way to solve problems like this.

        Now I have a question about how can this even work? In my example, I definitly place objects from type "table_cell" into the table cells, but I use a function (cellWidget) that searches for objects from type "QWidget"... I know, QWidget is a parent class of "table_cell", but it is still not the same, so how can this function return anything? I mean, if it's possible to mix types which are inherited from one to another, then it should also be possible to do it like this:
        @ table_cell* cell_amount = table->cellWidget(i, 0);@
        instead of:
        @QWidget* cell_amount = table->cellWidget(i, 0);@
        But no, then compiler throws an error:
        [quote]invalid conversion from QWidget* to table_cell*[/quote]
        Sounds logical, but why is this wrong but getting a table_cell instance with the same function is right?? The last one should also throw an error because the found Widget in the table isn't a QWidget but a table_cell object!
        I didn't get it...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Not dynamic_cast, qobject_cast ;)

          @table_cell* cell_amount = qobject_cast<table_cell*>(table->cellWidget(i,0));

          if (cell_amount) { << ensure that if you didn't return a table_cell your program doesn't crash
          //doSomething
          }
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on last edited by
            #5

            Hi again :)
            [quote]Not dynamic_cast, qobject_cast[/quote]
            Well, I tried both with same result... I thought qobject_cast would finally also use standard dynamic_cast? Or does qobject_cast have any benefits?

            Doesn't matter, what I do not understand is how it can even work... Why is it possible to on the one hand convert a table_cell object to a QWidget object, but on the other hand it isn't possible to convert a QWidget object to a table_cell object?
            And how is it possible to get members of a table_cell object out of a QWidget object after using dynamic_cast/qobject_cast? What happens while using this cast? How does compiler manage this?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The benefits are described in qobject_cast "documentation":http://qt-project.org/doc/qt-5/qobject.html#qobject_cast.

              It the first case there's no conversion, cell_widget is a QWidget.

              If qobject_cast<table_cell*> returns zero then either cellWidget returns 0 or returns something that is not a table_cell widget.

              Type casting is best explained "here":http://www.cplusplus.com/doc/tutorial/typecasting/

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved