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

DoubleClick event on specific fields in QSqlQueryModel



  • Hello Everyone,

    So I want to be able to capture a doubleclick event from a field within a QSqlQueryModel. Here is my code to create my voucher model:

    vouchermodel = new QSqlQueryModel;
    vouchermodel->setQuery(updateStr);
    
    vouchermodel->setHeaderData(0, Qt::Horizontal, QObject::tr("Voucher"));
    vouchermodel->setHeaderData(1, Qt::Horizontal, QObject::tr("Date"));
    vouchermodel->setHeaderData(2, Qt::Horizontal, QObject::tr("Amount"));
    vouchermodel->setHeaderData(3, Qt::Horizontal, QObject::tr("Due"));
    vouchermodel->setHeaderData(4, Qt::Horizontal, QObject::tr("G/L#"));
    vouchermodel->setHeaderData(5, Qt::Horizontal, QObject::tr("Vendor Inv"));
    vouchermodel->setHeaderData(6, Qt::Horizontal, QObject::tr("Check #"));
    vouchermodel->setHeaderData(7, Qt::Horizontal, QObject::tr("Check Date"));
    
    voucherTableView->setModel(vouchermodel);
    voucherTableView->resizeColumnsToContents();
    voucherTableView->verticalHeader()->hide();
    voucherTableView->show();
    connect(voucherTableView->selectionModel(), SIGNAL(currentVoucherChanged(const QModelIndex &,const QModelIndex &)),
            this, SLOT(currentReceiptChanged(const QModelIndex &)));
    

    So, for example, I want the user to be able to double click any voucher being displayed in this area, and I want to have a procedure set up that will open the voucher maintenance screen and have the voucher they clicked on be displayed. Additionally, I also might want to allow them to double click the vendor invoice field on the same record and pass that information to a procedure that might display a scanned copy of that invoice, and so on.

    How must i change my above code to allow for this to happen?



  • @Scott-Krise
    At the broadest level, are you clear that the UI interaction (e.g. double-click) will be handled through your voucherTableView (QTableView?), rather than the QSqlQueryModel you ask about? So that's where to look in docs. From there you will get the field in the model the user has clicked on, and pass that on to other code.



  • @Scott-Krise
    At the broadest level, are you clear that the UI interaction (e.g. double-click) will be handled through your voucherTableView (QTableView?), rather than the QSqlQueryModel you ask about? So that's where to look in docs. From there you will get the field in the model the user has clicked on, and pass that on to other code.



  • Ah, no...that wasn't clear. I thought it would be handled differently for the QSqlQueryModel...and I couldn't find any documentation on that. Ill see if I can find what I need in the documentation.

    Thanks!



  • As @JonB just said, generally speaking the view is what interacts with the user.

    Your view inherits from QAbstractItemView which has the signal you are looking for, it will send the QModelIndex that you can use to access the data and other stuff.

    Good luck :)



  • I got it working. Thank you!!


Log in to reply