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: How can I get the row and column index when user click on a particular cell using single mouse click?
QtWS25 Last Chance

QTableWidget: How can I get the row and column index when user click on a particular cell using single mouse click?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 5.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.
  • A Offline
    A Offline
    amarism
    wrote on last edited by
    #1

    I have created a TableWidget inside i have tale 4 row and 5 column. Now, I want to select particular row and column place where i can apply some another event. Here my peace of code for creating dynamically QTableWidget at proper place.

     QTableWidget *twidget = new QTableWidget(this);
      twidget->setStyleSheet(
    	"QTableWidget{"
    	"background-color: lightgray;"
    	"}");
    twidget->setGeometry(114, 70, 200, 98);
    twidget->clear();
    twidget->verticalHeader()->hide();
    twidget->horizontalHeader()->hide();
    twidget->setRowCount(4);
    twidget->setColumnCount(5);
    for (auto r = 0; r < 4; r++) {
    	for (auto c = 0; c < 5; c++) {
    		QTableWidgetItem *item = new QTableWidgetItem(QString::number(r + 1) + "x" + QString::number(c + 1));
    		item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
    		twidget->setItem(r, c, item);
    		//twidget->setItem(r, c, new QTableWidgetItem(QString::number(r+1) + "x" + QString::number(c+1)));
    		twidget->resizeRowsToContents();
    		twidget->resizeColumnsToContents();
    	}
    }
    twidget->show();
    

    Now I want to get the index of place wehere i can apply signal and slot to processing on the place.

    Thanku in advance

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      http://doc.qt.io/qt-5/qabstractitemview.html#clicked

      connect(twidget,&QAbstractItemView::clicked,this,[](const QModelIndex& idx)->void{
      qDebug() << "Clicked: " << idx.row()<<","<<idx.column();
      });
      

      P.S.

      twidget->setGeometry(114, 70, 200, 98);

      not a fan of this, any reason why you are not using a layout?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      A 1 Reply Last reply
      1
      • VRoninV VRonin

        http://doc.qt.io/qt-5/qabstractitemview.html#clicked

        connect(twidget,&QAbstractItemView::clicked,this,[](const QModelIndex& idx)->void{
        qDebug() << "Clicked: " << idx.row()<<","<<idx.column();
        });
        

        P.S.

        twidget->setGeometry(114, 70, 200, 98);

        not a fan of this, any reason why you are not using a layout?

        A Offline
        A Offline
        amarism
        wrote on last edited by amarism
        #3

        @VRonin I want to create one slot method where i can get row and column . inside the slop function i am using signal and slot to fix the proper method.
        Also i am used this one connect signal but this one not working.

        connect(twidget, SIGNAL(cellClicked(int, int)), this, SLOT(previousWeek(int, int)));
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          If you are using Qt5 you should move to the New connect syntax.

          change previousWeek from a slot that takes 2 ints to 1 that takes 1 const QModelIndex&
          then use connect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));.

          you can then use QModelIndex::row() and QModelIndex::column() to find out the coodinates of the clicked item

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          A 1 Reply Last reply
          2
          • VRoninV VRonin

            If you are using Qt5 you should move to the New connect syntax.

            change previousWeek from a slot that takes 2 ints to 1 that takes 1 const QModelIndex&
            then use connect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));.

            you can then use QModelIndex::row() and QModelIndex::column() to find out the coodinates of the clicked item

            A Offline
            A Offline
            amarism
            wrote on last edited by
            #5

            @VRonin said in QTableWidget: How can I get the row and column index when user click on a particular cell using single mouse click?:

            connect(twidget, SIGNAL(clicked(QModelIndex)), this, SLOT(previousWeek(QModelIndex)));.

            When i do this one in constructure then i will getting exception
            "Exception thrown at 0x00007FFAEB2F352C (Qt5Cored.dll) in RadSpaEngine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              Seems like you are accessing a dangling/unassigned pointer. where did you put the connect?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              A 1 Reply Last reply
              2
              • VRoninV VRonin

                Seems like you are accessing a dangling/unassigned pointer. where did you put the connect?

                A Offline
                A Offline
                amarism
                wrote on last edited by
                #7

                @VRonin Now i get the problem? Thanku for responding me

                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