Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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();
    
          });
    

  • Lifetime Qt Champion

    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 ?



  • @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 ?



  • 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
    
    ``

  • Lifetime Qt Champion

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

    What exact error are you getting ?



  • @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"


  • Lifetime Qt Champion

    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).



  • @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


  • Lifetime Qt Champion

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


Log in to reply