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. [SOLVED] Access a cellwidget of a tablewidget..
QtWS25 Last Chance

[SOLVED] Access a cellwidget of a tablewidget..

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.8k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    So lets say we have a tablewidget and we have added a qtimeedit to a cellwidget of the tablewidget via

    @QTimeEdit *end_time=new QTimeEdit(this);
    end_time->setTime(QTime(12,04));
    ui->tableWidget->setCellWidget(0, 3, end_time);@

    How can i access it later to see the current qtime?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      First option is to keep track of the widgets yourself, of course. But you can also access them using something like:
      @
      QModelIndex index = tableWidget->model()->index(row, column);
      QWidget* theCellWidget = tableWidget->indexWidget(index);
      @

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        [quote author="Andre" date="1380538325"]First option is to keep track of the widgets yourself, of course. But you can also access them using something like:
        @
        QModelIndex index = tableWidget->model()->index(row, column);
        QWidget* theCellWidget = tableWidget->indexWidget(index);
        @
        [/quote]

        Yes but u can't use thecellwidget->time() because we didn't declared that the cellwidget it a qtimeedit..

        I tried someting like
        @QModelIndex index = ui->tableWidget->model()->index(i, 2);
        QTimeEdit* theCellWidget = ui->tableWidget->indexWidget(index);@

        but gives an error of
        @C2440: 'initializing' : cannot convert from 'QWidget *' to 'QTimeEdit *'
        Cast from base to derived requires dynamic_cast or static_cast@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Well, the error is quite clear, I'd say. It even proposes a solution... Note that you won't have this problem if you keep track of the widgets yourself.

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

            Hi,

            You need to use qobject_cast, (since you are using QObject derived classes) you can't just assign a pointer from one type to another like that

            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
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #6

              Yep this worked

              @QModelIndex index = ui->tableWidget->model()->index(i, 2);
              QTimeEdit* theCellWidget = qobject_cast<QTimeEdit *>(ui->tableWidget->indexWidget(index));
              qDebug () << theCellWidget->time().toString();@

              ;)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                Line 3 will cause a crash if line 2 failed. Always check the result of a dynamic cast (and qobject_cast is a dynamic cast)!

                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