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. I need to get the row number in QTableView when i click the ViewProfile Button

I need to get the row number in QTableView when i click the ViewProfile Button

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.0k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    MostafaEzzat
    wrote on last edited by aha_1980
    #1

    I need to get the row numbr in QTableView when i click the ViewProfile Button following by other things but how to pass the right parameters to connect()

    QSignalMapper *signalMapper = new QSignalMapper(this);
       //   profile = new Profile(this) ;
          QPushButton *ViewProfile ;
          QWidget *w = qobject_cast<QWidget *>(sender()->parent());
          profile = new(Profile);
    
     for( int i=0; i<7; i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
          //do something with your data for normal cells...
          auto item = model->index(i, 6);
       //   model->setData(item, QVariant::fromValue(model->getSpecificInformation()));
          //make new button for this row
          item = model->index(i, 6);
     ViewProfile = new QPushButton("ViewProfile");
          view->setIndexWidget(item, ViewProfile);
          signalMapper->setMapping(ViewProfile, profile);
         connect(ViewProfile, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
     }
     view->show();
     QString name =  view->model()->data(view->model()->index(0,0)).toString();
    
          qDebug() << name ;
          //   profile->label_3_nameP  =  name ;
          connect(signalMapper, QOverload<profile>(QSignalMapper::mapped),
              [=](profile)
          {
              int row =  view->indexAt(w->pos()).row();
               qDebug() << row ;
               QString name =  view->model()->data(view->model()->index(row,0)).toString();
               qDebug() << name ;
              profile->show();
    
          });
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The title of a thread is not the place to put your question body, please make it short and on point. Then write the complete question in the body of the question. So please fix that.

      As for your problem, I would be surprised that the code is building. Is it ?

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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        The title of a thread is not the place to put your question body, please make it short and on point. Then write the complete question in the body of the question. So please fix that.

        As for your problem, I would be surprised that the code is building. Is it ?

        M Offline
        M Offline
        MostafaEzzat
        wrote on last edited by MostafaEzzat
        #3

        @SGaist
        Hi ,
        no the code not built i have a problem in this line

             connect(signalMapper, QOverload<profile>(QSignalMapper::mapped),
                 [=](profile)
         
        

        connect(signalMapper, QOverload<QObject *>::of(&QSignalMapper::mapped),
        [=](QObject object){ / ... */ });

        i don't which parameter should i pass it gives me
        "profile not a type " "QOverload was not declared in this scope"

        so i gave profile for both object but i guess i was wrong i should have passed ViewProfile right ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MostafaEzzat
          wrote on last edited by
          #4

          This is the full function of delgate.cpp

          #include "delgate.h"
          
          MyDelegate::MyDelegate(QObject *parent)
           {
          
          }
          void MyDelegate:: qtable(){
          
              QSqlDatabase  Databasee =   QSqlDatabase::addDatabase("QMYSQL" , "thirdConnection");
              Databasee.setHostName("localhost") ;
              Databasee.setUserName("root");
              Databasee.setPassword("mostafa");
              Databasee.setDatabaseName("Clinic_Database");
          
          // Pls Notice that 'QSqlTableModel' doesn't work without making sure that database connected
          
              if(Databasee.open()){
                QMessageBox::about(this,"res","databaseopen") ;
          
          
              }else {
                  QMessageBox::about(this,"res","database not open") ;
                  qDebug() <<  Databasee.lastError();
              }
          
              model = new QSqlTableModel(this,  Databasee);
              model->setTable("PatientsInfo");
              model->setEditStrategy(QSqlTableModel::OnManualSubmit);
             model->select();
             model->setHeaderData(0, Qt::Horizontal, QObject::tr("PatientsName"));
             model->setHeaderData(1, Qt::Horizontal, QObject::tr("Age")) ;
             model->setHeaderData(2, Qt::Horizontal, QObject::tr("BloodPrint"));
             model->setHeaderData(3, Qt::Horizontal, QObject::tr("PhoneNum"));
             model->setHeaderData(4, Qt::Horizontal, QObject::tr("DiagnosisDisease"));
             model->setHeaderData(5, Qt::Horizontal, QObject::tr("consultant"));
             model->setHeaderData(6, Qt::Horizontal, QObject::tr("ViewProfile"));
          
            view = new QTableView;
             view->setModel(model);
          
              QSignalMapper *signalMapper = new QSignalMapper(this);
               //   profile = new Profile(this) ;
                  QPushButton *ViewProfile ;
                  QWidget *w = qobject_cast<QWidget *>(sender()->parent());
                  profile = new(Profile);
          
             for( int i=0; i<7; i++ ) { //replace rows.length with your list or vector which consists of the data for your rows.
                  //do something with your data for normal cells...
                  auto item = model->index(i, 6);
               //   model->setData(item, QVariant::fromValue(model->getSpecificInformation()));
                  //make new button for this row
                  item = model->index(i, 6);
             ViewProfile = new QPushButton("ViewProfile");
                  view->setIndexWidget(item, ViewProfile);
                  signalMapper->setMapping(ViewProfile, profile);
                 connect(ViewProfile, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
             }
             view->show();
             QString name =  view->model()->data(view->model()->index(0,0)).toString();
          
                  qDebug() << name ;
                  //   profile->label_3_nameP  =  name ;
                  connect(signalMapper, QOverload<ViewProfile>(QSignalMapper::mapped),
                      [=](ViewProfile)
                  {
                      int row =  view->indexAt(w->pos()).row();
                       qDebug() << row ;
                       QString name =  view->model()->data(view->model()->index(row,0)).toString();
                       qDebug() << name ;
                      profile->show();
          
                  });
          
          
          }
          

          this is the class and Profile is another window to show some data in QTableView after clicking the button

          #ifndef DELGATE
          #define DELGATE
          #include <QtGui>
          #include <QModelIndex>
          #include <QObject>
          #include <QSqlTableModel>
          #include <QtSql>
          #include <QSqlDatabase>
          #include <QSqlQueryModel>
          #include <QSqlQuery>
          #include <QPushButton>
          #include <QMessageBox>
          #include <QTableView>
          #include <QPoint>
          #include "profile.h"
          class MyDelegate : public QWidget
          {
              Q_OBJECT
          
          public:
               MyDelegate(QObject *parent = 0);
              QSqlTableModel * model ;
              QTableView * view ;
              void qtable() ;
               Profile * profile ;
          
          private:
          
          
          };
          
          #endif // DELGATE
          
          ``
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            There's no such overload for QSignalMapper::mapped.

            What exact error are you getting ?

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

            M 1 Reply Last reply
            0
            • SGaistS SGaist

              There's no such overload for QSignalMapper::mapped.

              What exact error are you getting ?

              M Offline
              M Offline
              MostafaEzzat
              wrote on last edited by MostafaEzzat
              #6

              @SGaist

              i guess i have misunderstand in using this function but i only need when i click ViewProfile button in QTableView do some stuff one of them getting the index of the row of the button to get the data from the row or just give an article or an Example for how to do this cos i searched on the interner and didn't find similar one.. Thanks in Advance

              this is an url of the image for the buttons in QTableView : "https://ibb.co/qp6wkbB"

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

                You have the example on how to use it in QSignalMapper's documentation but in your case, it seems it would make more sense to use void QSignalMapper::mapped(int i).

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

                M 1 Reply Last reply
                0
                • SGaistS SGaist

                  You have the example on how to use it in QSignalMapper's documentation but in your case, it seems it would make more sense to use void QSignalMapper::mapped(int i).

                  M Offline
                  M Offline
                  MostafaEzzat
                  wrote on last edited by
                  #8

                  @SGaist

                  i'm sorry i'm a little confused .. but which i need is making the button calling itself like making a button a function has body that can i put some codes inside it and the SLOT will be the button(ViewProfile) as well .. i tried to make the button execute another function but it won't been working coz i need QTableView in same function otherwise i'll lose the data and also can't pass a pointer of the TableView to another function to have the same data or Result

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

                    If your slot is in the same class as the buttons and QTableView, then you already have everything needed.

                    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