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. QTableWidget - get cell index from cell widget ?
QtWS25 Last Chance

QTableWidget - get cell index from cell widget ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey

    I have a QLineEdit in my cell that when conencted to return pressed emits self to my function.

    In my function I try doing indexAt(mapToGlobal(mySender->pos())); and then somehow get the index location of where my widget is at. But that does not work... I tried converting from global coordinate to local and even mapTo(xx) but no luck. How can I properly map the coordinate ? Or maybe there is a better way of doing it ?

    Regards
    Dariusz

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

      Hi
      If you know what cell index is when you call
      setWidget to apply the QLineEdit, you could store it
      in a property
      thelineedit->setProperty("index", xxx)
      and then read it in your function.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dariusz
        wrote on last edited by
        #3

        Except that when I drag drop etc I will lose that data. I have also partly custom drag drop system as well.... So if I could get it from indexAt that would help a lot...

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          indexAt() is working correct - when you pass the correct (viewport) coordinates to it...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

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

            Hi
            Did a fast test.
            alt text
            QLineEdits at

                ui->tableWidget->setCellWidget(1, 1, AddEdit());
                ui->tableWidget->setCellWidget(2, 2, AddEdit());
                ui->tableWidget->setCellWidget(3, 4, AddEdit());
            

            Then in slot

            void MainWindow::OnEnter()
            {
                QLineEdit *line = qobject_cast<QLineEdit *> ( sender() );
                if (!line) return;
                qDebug() << line->pos();
                qDebug() <<  ui->tableWidget->indexAt( line->pos() );
            }
            

            and output is

            QPoint(100,30)
            QModelIndex(1,1,0x0,QTableModel(0x1f1d13ea7d0))
            QPoint(200,60)
            QModelIndex(2,2,0x0,QTableModel(0x1f1d13ea7d0))
            QPoint(400,90)
            QModelIndex(3,4,0x0,QTableModel(0x1f1d13ea7d0))
            

            Which seems pretty ok to me ?

            1 Reply Last reply
            3
            • D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #6

              Hmmmmmmmmm that got me to think a bit more....

              Since my lineEdit was inside a QWidget layouts as child... I tried qDebug() << indexAt(dynamic_cast<QWidget *>(sender->parent())->pos()); and wuaila it worked!

              Hmmmmmm strange that I could not get it directly from QLineEdit but I had to go to top parent widget... ?

              Still works, thanks so much for help!

              mrjjM 1 Reply Last reply
              1
              • D Dariusz

                Hmmmmmmmmm that got me to think a bit more....

                Since my lineEdit was inside a QWidget layouts as child... I tried qDebug() << indexAt(dynamic_cast<QWidget *>(sender->parent())->pos()); and wuaila it worked!

                Hmmmmmm strange that I could not get it directly from QLineEdit but I had to go to top parent widget... ?

                Still works, thanks so much for help!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Dariusz
                Just a note
                qobject_cast is preferable to dynamic_cast.

                If the QLineEdit is inside a widget, it means its pos() will be the offset from
                the QWidget (parent) which would be like just a few pixels. ( like 9,9 )
                However, taking Pos() on the parent widget get you the position relative to the TableWidget which is needed for indexAt()

                1 Reply Last reply
                3

                • Login

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