SQL Parameter count mismatch on QTableView - c++

I have a GUI composed of:
N.1 GraphicsView
N.1 QTableView
N.1 dialog that opens up as needed by the user after doubleclicking on each row of the QTableView
I can capture features on the QGraphicsView with the mouse click. After the feature is drawn I right click and open up a dialog like the one in the figure:
After hitting accept: I register the feature as the row index of the QTableView as shown below:
If I doubleclick on each row I am able to open again the same dialog with the information previously saved. I do that because I may need to change the name of the image and call it differently.
The problem I currently have is that I ma receiving a weird Parameter count mismatch from the compiler and I don't understand why and it should be due to QSQLITE
Below is what I am trying to do along with the most important part of the code:
parameters.h
typedef struct Param
{
int mId;
QString mName;
QByteArray mSave;
} Param;
class Parameters
{
public:
Parameters() = default;
Parameters(Param newdata);
Parameters(int id, const QString &name, const QByteArray &save);
int id() const { return data.mId; }
QString name() const {return data.mName; }
QByteArray save() const {return data.mSave; }
Param getData() const { return data; }
void setData(Param ndat) { data = ndat; }
private:
Param data;
};
parameters.cpp
#include "parameters.h"
Parameters::Parameters(Param newdata) {
data = newdata;
}
Parameters::Parameters(int id, const QString &name,
const QByteArray &save) {
data.mId = id;
data.mSave = save;
data.mName = name;
}
the database is set in the following dataleftcamera.h part:
public:
explicit dataLeftCamera(QObject *parent = nullptr);
bool addItem(Parameters* mParameters);
bool updateItem(int itemId, Parameters* mParameters);
QSqlDatabase getDatabase();
private:
QString mError;
QSqlDatabase mDatabaseLeft;
The table is initiated here on dataleftcamera.cpp and here is where the compiler is giving the Parameter count mismatch error specifically on the function updateItem
#define CREATE_TABLE \
" CREATE TABLE IF NOT EXISTS leftCamTable" \
" (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" \
", name TEXT NOT NULL" \
", save BLOB NOT NULL)"
dataLeftCamera::dataLeftCamera(QObject *parent) : QObject (parent)
{}
bool dataLeftCamera::addItem(Parameters *mParameters)
{
QSqlQuery qry;
qry.prepare("INSERT INTO leftCamTable (name, save)"\
" VALUES (?,?)");
qry.addBindValue(mParameters->name());
qry.addBindValue(mParameters->save());
bool ok = qry.exec();
if(!ok) {
mError = qry.lastError().text();
qDebug() << mError;
}
}
bool dataLeftCamera::updateItem(int itemId, Parameters *mParameters)
{
QSqlQuery qry;
qry.prepare(" UPDATE lefCamTable SET " \
" name = ?," \
" save = ?" \
" WHERE id = ?");
qry.addBindValue(mParameters->name());
qry.addBindValue(mParameters->save());
qry.addBindValue(itemId);
bool ok = qry.exec();
if(!ok) {
mError = qry.lastError().text();
qDebug() << mError;
}
}
On the mainwindow.cpp is the part where I update the item after I doubleclick on the row to change the name of the image and accept again the modification:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mDatabaseLeftCamera = new dataLeftCamera(this);
mModelLeftCamera = nullptr;
mModelLeftCamera = new QSqlTableModel(this);
ui->tableView->setModel(mModelLeftCamera);
connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(onTableClick(QModelIndex)));
// temporary folder
temporaryFolder = "/home/name/Desktop/tempDBFolder/tmp.db";
QFile dbRem(temporaryFolder);
dbRem.remove();
mDatabaseLeftCamera->inizializationDatabaseLeftCamera(temporaryFolder);
mDatabaseLeftCamera->configurationDatabaseLeftCamera();
mModelLeftCamera = new QSqlTableModel(this, mDatabaseLeftCamera->getDatabase());
mModelLeftCamera->setTable("leftCamTable");
mModelLeftCamera->select();
ui->tableView->setModel(mModelLeftCamera);
ui->tableView->showColumn(true);
}
// opening the dialog for the first time after capturing the image
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
// operations
Param result = d.getData();
Parameters* param = new Parameters(result);
mDatabaseLeftCamera->addItem(param);
mModelLeftCamera->select();
ui->tableView->show();
}
// This is the doubleclick that re-opens the small dialog to change the name of the feature
void MainWindow::onTableClick(const QModelIndex &index)
{
int row = index.row();
Param currentData;
int ItemId = index.sibling(row, 0).data().toInt();
currentData.mName = index.sibling(row, 1).data().toString();
currentData.mSave = index.sibling(row, 2).data().toByteArray();
QPixmap iconPix;
if(!iconPix.loadFromData(index.sibling(row, 2).data().toByteArray())) {
}
clipSceneDialog d(this);
d.show();
d.setData(currentData);
d.setImage(iconPix.toImage());
if(d.exec() == QDialog::Rejected) {
return;
} else {
//
}
Param result = d.getData();
Parameters* param = new Parameters(result);
mDatabaseLeftCamera->updateItem(ItemId,param);
mModelLeftCamera->select();
ui->tableView->show();
}
For completeness see the error the compiler is giving if that helps:
I am sorry if this is trivial but I checked the updateItem(ItemId,param)
and thanks for shedding light on this.

Try and add some debug printing. Especially for the parameters that you add to the prepared statement. qry.addBindValue converts your value to a QVariant. Based on the documentation QVariants become NULL when they contain no data:
QVariant x(QString());
// x.isNull() == true
In case there is a problem retrieving your parameters, this could explain the violation of the NOT NULL constraints.

Related

QCompleter and QSortFilterProxyModel - what to do to be able to choose which column to search from

I made a model for data mining in TableView - the code looks something like this and runs in the constructor of the MainWindow class
(...)
leEdtUserSearch = new QLineEdit(this);
modelSqlBorrowingUsers = new SqlQueryModelBorrowingUsers(this, accessDB); //my model inheriting from QSqlQueryModel
tbViewUserList = new QTableView(this); //model table view above
srtFltrMdl = new QSortFilterProxyModel(this);
cmpltrNameAndSurname = new QCompleter(this);
srtFltrMdl->setSourceModel(modelSqlBorrowingUsers);
tbViewUserList->setModel(srtFltrMdl);
tbViewUserList->setColumnHidden(0,true);
cmpltrNameAndSurname->setModel(modelSqlBorrowingUsers);
leEdtUserSearch->setCompleter(cmpltrNameAndSurname);
QObject::connect(bttnGrpUserSearch, &QButtonGroup::idClicked, this, &MainWindow::searchByFirstNameOrLastName); //slot to search from the selected column
//below is theoretically correct search but ... something doesn't work
QObject::connect(leEdtUserSearch, &QLineEdit::textChanged, srtFltrMdl, qOverload<const QString &>(&QSortFilterProxyModel::setFilterRegularExpression));
(...)
slot to search from the selected column
void MainWindow::searchByFirstNameOrLastName(int idSearch)
{
if(idSearch==1){
srtFltrMdl->setFilterKeyColumn(1);
}
else if(idSearch==2){
srtFltrMdl->setFilterKeyColumn(2);
}
}
I probably know what could be wrong, the error may be in the model function - the data seems to be returned correctly but QCompleter and QSortFilterProxyModel see them as empty and I don't know why. Any of you see a bug here?
QVariant SqlQueryModelBorrowingUsers::data(const QModelIndex &index, int role) const
{
if(this->query().isSelect()){
if(this->query().first() && this->query().isValid()){
if(this->query().isActive()){
QVariant myData;
if(role==Qt::EditRole || role==Qt::DisplayRole){
myData = QSqlQueryModel::data(index, role); //here somehow strangely returned data that QCompleter and QSortFilterProxyModel cannot see
}
if(role==Qt::FontRole && index.column()==1){
QFont font;
font.setWeight(QFont::ExtraBold);
myData = font.toString();
}
if(role==Qt::TextAlignmentRole && (index.column()==1 || index.column()==2)){
return Qt::AlignCenter;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 0){
QColor color;
color.setRgb(242,242,242,255);
myData = color;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 1){
QColor color;
color.setRgb(217,217,217,170);
myData = color;
}
return myData;
}
}
}
else if(this->query().isValid()){
return QVariant();
}
return QVariant();
}
where is the error and how to correct it?
EDIT
#Alexey
Please, show us how and where QSqlQuery is set to the model
the code of the entire model
#include "sqlquerymodelborrowingusers.h"
#include <QDebug>
SqlQueryModelBorrowingUsers::SqlQueryModelBorrowingUsers(QObject *parent, QSqlDatabase *dbConnect) : QSqlQueryModel{parent}
{
db = dbConnect;
QString userData = "SELECT pIDuserWyp, pImie, pNazwisko FROM 'tUzytkownicyWypozyczajacy'";
this->setQuery(userData, *dbConnect);
this->query().exec();
}
int SqlQueryModelBorrowingUsers::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
int row=0;
QString rows = "SELECT count(*) FROM 'tUzytkownicyWypozyczajacy'";
QSqlQuery queryRow(*db);
queryRow.exec(rows);
QSqlRecord recRow;
if(queryRow.first()){
recRow = queryRow.record();
row = recRow.value(0).toInt();
return row;
}
else{
return row;
}
return row;
}
int SqlQueryModelBorrowingUsers::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
int column=0;
QString columns = "SELECT count(*) FROM PRAGMA_table_info('tUzytkownicyWypozyczajacy')";
QSqlQuery queryColumn(*db);
queryColumn.exec(columns);
QSqlRecord recColumn;
if(queryColumn.first()){
recColumn = queryColumn.record();
column = recColumn.value(0).toInt();
return column;
}
else{
return column;
}
return column;
}
QVariant SqlQueryModelBorrowingUsers::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role==Qt::DisplayRole && orientation==Qt::Horizontal){
if(section==1){
QVariant header1;
header1="Name";
return header1;
}
if(section==2){
QVariant header2;
header2="Surname";
return header2;
}
}
if(role==Qt::DisplayRole && orientation==Qt::Vertical){
return QString("%1").arg(section+1);
}
return QVariant();
}
QVariant SqlQueryModelBorrowingUsers::data(const QModelIndex &index, int role) const
{
if(this->query().isSelect()){
if(this->query().first() && this->query().isValid()){
if(this->query().isActive()){
QVariant myData;
if(role==Qt::EditRole || role==Qt::DisplayRole){
myData = QSqlQueryModel::data(index, role);
}
if(role==Qt::FontRole && index.column()==1){
QFont font;
font.setWeight(QFont::ExtraBold);
myData = font.toString();
}
if(role==Qt::TextAlignmentRole && (index.column()==1 || index.column()==2)){
return Qt::AlignCenter;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 0){
QColor color;
color.setRgb(242,242,242,255);
myData = color;
}
if(role==Qt::BackgroundRole && index.row() % 2 == 1){
QColor color;
color.setRgb(217,217,217,170);
myData = color;
}
return myData;
}
}
}
else if(this->query().isValid()){
return QVariant();
}
return QVariant();
}
bool SqlQueryModelBorrowingUsers::setData(const QModelIndex &index, const QVariant &value, int role)
{
QModelIndex pID = QSqlQueryModel::index(index.row(),0,QModelIndex());
int _pID = data(pID,Qt::EditRole | Qt::DisplayRole).toInt();
bool isSet=false;
if(!index.isValid()){
return false;
}
else if(index.column()<1){
return false;
}
else{
if(role==Qt::EditRole || role==Qt::DisplayRole){
if(index.column()==1){
isSet = name(_pID, value.toString());
emit dataChanged(index,index,{Qt::EditRole | Qt::DisplayRole});
this->refresh();
return isSet;
}
if(index.column()==2){
isSet = surname(_pID, value.toString());
emit dataChanged(index,index,{Qt::EditRole | Qt::DisplayRole});
this->refresh();
return isSet;
}
}
}
return isSet;
}
Qt::ItemFlags SqlQueryModelBorrowingUsers::flags(const QModelIndex &index) const
{
Qt::ItemFlags flag = QSqlQueryModel::flags(index); //export domyślnie ustawionych flag przez klasę nadrzędną
if(!index.isValid()){
return Qt::NoItemFlags;
}
else if(index.isValid()){
if(index.column()==1 || index.column()==2){
flag |= Qt::ItemIsEditable; //dopisanie nowej flagi do wcześniej wyeksportowanych flag
return flag;
}
}
return flag;
}
bool SqlQueryModelBorrowingUsers::name(int pID, const QString &pName)
{
QSqlQuery queryUpdateName;
QVariant _pID;
_pID = pID;
QString strUpdateName = "UPDATE 'tUzytkownicyWypozyczajacy' SET pImie = ? WHERE pIDuserWyp = ?";
queryUpdateName.prepare(strUpdateName);
queryUpdateName.addBindValue(pName);
queryUpdateName.addBindValue(_pID.toString());
return queryUpdateName.exec();
}
bool SqlQueryModelBorrowingUsers::surname(int pID, const QString &pSurname)
{
QSqlQuery queryUpdateSurname;
QVariant _pID;
_pID = pID;
QString strUpdateSurname = "UPDATE 'tUzytkownicyWypozyczajacy' SET pNazwisko = ? WHERE pIDuserWyp = ?";
queryUpdateSurname.prepare(strUpdateSurname);
queryUpdateSurname.addBindValue(pSurname);
queryUpdateSurname.addBindValue(_pID.toString());
return queryUpdateSurname.exec();
}
void SqlQueryModelBorrowingUsers::myAddRow()
{
QSqlQuery lastID, addRecord;
QString strGetID = "SELECT pIDuserWyp FROM 'tUzytkownicyWypozyczajacy'";
lastID.prepare(strGetID);
if(lastID.exec()){
if((lastID.last() && lastID.isValid()) || !lastID.isValid()){
QString strAddRecord = "INSERT INTO 'tUzytkownicyWypozyczajacy' (pImie, pNazwisko) VALUES (:Imie,:Nazwisko)";
if(this->pName.isEmpty() && !this->pSurname.isEmpty()){
emit this->messageOfNumber(1);
}
else if(this->pSurname.isEmpty() && !this->pName.isEmpty()){
emit this->messageOfNumber(2);
}
else if(this->pName.isEmpty() && this->pSurname.isEmpty()){
emit this->messageOfNumber(3);
}
else{
addRecord.prepare(strAddRecord);
addRecord.bindValue(":Imie",this->pName);
addRecord.bindValue(":Nazwisko",this->pSurname);
addRecord.exec();
addRecord.clear();
pName.clear();
pSurname.clear();
refresh();
emit this->userDataIsSet(); //sygnał służący do wyczyszczenia QLineEditów po dodaniu danych do bazy
emit this->messageOfNumber(0); //syngał służący do uruchomienia odpowiedniego komunikatu o błędzie
}
}
}
else{
emit this->messageOfNumber(-1);
}
}
void SqlQueryModelBorrowingUsers::myRemoveRow()
{
QModelIndex getIDfromTable;
QVariant getIDtoRemove;
QSqlQuery currentID, removeRow;
QString strGetCurrentID = "SELECT pIDuserWyp FROM 'tUzytkownicyWypozyczajacy'";
currentID.prepare(strGetCurrentID);
int i=0;
if(currentID.exec()){
if(currentID.isSelect()){
if(currentID.isActive()){
while (currentID.next()){
i=currentID.at();
if(i==this->row){
getIDfromTable=index(i,0,QModelIndex());
getIDtoRemove=getIDfromTable.data();
}
}
}
}
}
QString strRemoveRow = "DELETE FROM 'tUzytkownicyWypozyczajacy' WHERE pIDuserWyp = (?)";
removeRow.prepare(strRemoveRow);
removeRow.addBindValue(getIDtoRemove.toString(),QSql::In);
removeRow.exec();
refresh();
}
void SqlQueryModelBorrowingUsers::refresh()
{
this->query().clear();
this->setQuery("SELECT pIDuserWyp, pImie, pNazwisko FROM 'tUzytkownicyWypozyczajacy'");
this->query().exec();
}
void SqlQueryModelBorrowingUsers::setName(const QString &pName)
{
this->pName = pName;
}
void SqlQueryModelBorrowingUsers::setSurname(const QString &pSurname)
{
this->pSurname = pSurname;
}
SqlQueryModelBorrowingUsers::~SqlQueryModelBorrowingUsers()
{
this->query().clear();
}
EDIT
#Alexey
What is purpose to pass QSqlDatabase to the model's constructor? Moreover you pass it as pointer. Why, if you can get it anywhere by calling static method QSqlDatabase::database, see docs?
I suggest you to remove QSqlDatabase *dbConnect from constructor arguments.
I pass the connection indicator with the base to the model because I have made such a class and I have one question about it.
See the code and tell me if I did it right
AccessToDB::AccessToDB()
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("127.0.0.1");
db.setPort(-1);
db.setDatabaseName("dbSpIT.db");
db.setUserName("");
db.setPassword("");
}
bool AccessToDB::connectDB()
{
if(db.isValid()==false){
errorConnectDB = db.lastError();
qDebug()<< errorConnectDB.text() << "load driver";
for(int i=0; i<db.drivers().count(); ++i){
qDebug()<< db.drivers().at(i);
}
return false;
}
else{
qDebug()<< "the database driver has been properly initialized";
if(db.open()==true){
qDebug()<< "database is open" << db.open() << db.connectionName();
return true;
}
else{
qDebug()<< "database not open";
return false;
}
}
}
AccessToDB::~AccessToDB()
{
db.close();
}
in the MainWindow class, this way I create a connection to the base
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
(...)
accessDB = new AccessToDB();
accessDB->connectDB();
(...)
//at this point I pass the connection from the base to the model
modelSqlBorrowingUsers = new SqlQueryModelBorrowingUsers(this, accessDB);
(...)
}
#Alexey
The same applies to the refresh method.
How can I refresh the view if I have added data to the database?
Ok, let's start with your model class first.
I do not review your model editing, only data reading.
Remove ' (apostrophes) from query strings.
What is purpose to pass QSqlDatabase to the model's constructor? Moreover you pass it as pointer. Why, if you can get it anywhere by calling static method QSqlDatabase::database, see docs?
I suggest you to remove QSqlDatabase *dbConnect from constructor arguments.
Everywhere where you need a db connection, you should call QSqlDatabase::database.
And you should not store it in a model class.
Further
this->setQuery(userData, *dbConnect);
this->query().exec();
According to the docs setQuery method
Executes the query query for the given database connection db
So, remove this->query().exec();. The same applies to the refresh method.
Remove rowCount method, you do not need to override the one implemented in base class, it returns a query size by default.
columnCount should return the count of columns of the query. Your query SELECT pIDuserWyp, pImie, pNazwisko FROM 'tUzytkownicyWypozyczajacy' has 3 columns, so your columnCount reimplementation should return 3.
Now, let's look at reimplemented data method.
Remove all this unnecessary check-outs
if(this->query().isSelect()){
if(this->query().first() && this->query().isValid()){
if(this->query().isActive()){
Your query is a SELECT query, it is valid (it should), it's active, because it is so.
Regarding the use of your model class. Do not share the same instance between different "consumers" (QSortFilterProxyModel, QCompleter, QLineEdit), create the new one for every of them.

Qt ListView doesn't show C++ model content

I'm creating a QList<> in C++ using a QML ListView to display it. The application runs without errors but the ListView stubbornly remains empty.
The QML will show a rectangle for the presence of each list item.
I checked the UI code by creating a list in QML.
It displays correctly for the QML created list.
Here's my QML:
import Processes 1.0
...
ListView {
id: qInterfaceList
height: parent.height;
width: parent.width;
model: myModel
orientation: ListView.Vertical
delegate:
Rectangle {
height: 30;
width: 120;
border.color: "red"
border.width: 3
}
The C++ code that creates and registers the list object:
// Register C++ classes as a QML type named Processes (version 1.0)
qmlRegisterType<Process>("Processes", 1, 0, "Process");
QQmlApplicationEngine engine;
// read the configuration file
Config conf;
if ( conf.read() )
{
QQmlContext* ctxt = engine.rootContext();
if ( ctxt )
{
qDebug()
<< "--- conf.Interfaces: "
<< conf.Interfaces.length()
;
ConfigInterface c;
QVariant v = QVariant::fromValue( conf.Interfaces );
qDebug()
<< "--- ConfigInterface: "
<< v
<< "--- typeName: "
<< v.typeName()
;
ctxt->setContextProperty("myModel", QVariant::fromValue( conf.Interfaces ));
}
}
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
To debug I output information about list from C++ and QML:
In C++ the count of list items is correct.
In C++ the conversion to QVariant is working.
In QML it sees the defined list.
The debugging output:
Debugging starts
--- conf.Interfaces: 65
--- ConfigInterface: QVariant(QList<ConfigInterface*>, ) --- typeName: QList<ConfigInterface*>
qml: myModel: QVariant(QList<ConfigInterface*>)
Debugging has finished
Any ideas what's wrong or how to debug this?
Thanks
EDIT: Here's the class being used as a list item
Class declaration:
class ConfigInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(QString sql READ getTag WRITE setTag NOTIFY tagChanged)
Q_PROPERTY(QString description READ getDescription WRITE setDescription NOTIFY descriptionChanged)
public:
/*explicit*/ ConfigInterface();
/*explicit*/ ConfigInterface(QObject *parent);
~ConfigInterface();
// Copy constructor needed because these are copied when added to a QList
ConfigInterface(const ConfigInterface &p2) {_tag = p2._tag; _description = p2._description; }
QString getDescription() const;
void setDescription(QString&);
QString getTag() const;
void setTag(QString&);
signals:
void tagChanged(QString);
void descriptionChanged(QString);
public:
QString _tag;
QString _description;
QString QueryTemplate;
QString ConnectString;
QString MinimumId;
};
Q_DECLARE_METATYPE(ConfigInterface*)
C++ code:
ConfigInterface::ConfigInterface()
: QObject( nullptr )
{
}
ConfigInterface::ConfigInterface(QObject* parent)
: QObject(parent)
{
}
ConfigInterface::~ConfigInterface()
{
}
QString ConfigInterface::getTag() const
{
return _tag;
}
void ConfigInterface::setTag(QString& str)
{
_tag = str;
emit tagChanged(_tag);
}
The main problem is caused because it has a list of ConfigInterface *, according to the examples provided in the documentation should be a list of QObject *:
class Config{
[...]
public:
QList<QObject *> Interfaces;
[...]
};
In addition to this you should get the following warning:
/..../configinterface.h:17: warning: base class ‘class QObject’ should be explicitly initialized in the copy constructor [-Wextra]
ConfigInterface(const ConfigInterface &p2) {_tag = p2._tag; _description = p2._description; }
^~~~~~~~~~~~~~~
This is caused because QObject and its derived classes must not have a copy constructor or assignment operator, For more information read the following:
http://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator
Another improvement is that both constructors can be united in only one, in the end their class could have the following structure:
configinterface.h
#ifndef CONFIGINTERFACE_H
#define CONFIGINTERFACE_H
#include <QObject>
class ConfigInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(QString sql READ getTag WRITE setTag NOTIFY tagChanged)
Q_PROPERTY(QString description READ getDescription WRITE setDescription NOTIFY descriptionChanged)
public:
ConfigInterface(QObject *parent=Q_NULLPTR);
~ConfigInterface();
QString getTag() const;
void setTag(const QString &tag);
QString getDescription() const;
void setDescription(const QString &description);
signals:
void tagChanged(QString);
void descriptionChanged(QString);
private:
QString _tag;
QString _description;
QString QueryTemplate;
QString ConnectString;
QString MinimumId;
};
#endif // CONFIGINTERFACE_H
configinterface.cpp
#include "configinterface.h"
ConfigInterface::ConfigInterface(QObject* parent)
: QObject(parent)
{
}
ConfigInterface::~ConfigInterface()
{
}
QString ConfigInterface::getDescription() const
{
return _description;
}
void ConfigInterface::setDescription(const QString &description)
{
if(_description == description)
return;
emit descriptionChanged(description);
_description = description;
}
QString ConfigInterface::getTag() const
{
return _tag;
}
void ConfigInterface::setTag(const QString &tag)
{
if(tag == _tag)
return;
emit tagChanged(tag);
_tag = tag;
}

Sample code for dialog like Qt Creator Options tab

I would like to build up dialog same as Qt Creator "Options" tab that left side page titles with scroll bar and detailed page on the right side.
It would be really helpful if there were code samples or sample applications for reference.
Qt Creator Source Code
Qt Creator has it source code both in Gitorious and in GitHub. But because Qt Creator is such a large and complex project, it can be overwhelming finding its sub parts.
The Github version is very searchable. It ends up that the source related to the nice options page in Qt Creator uses the IOptionsPage as the base class for any of the pages that show up in the Options dialog.
https://github.com/qtproject/qt-creator/search?utf8=%E2%9C%93&q=ioptionspage&type=Code
The ioptionspage.cpp has all the comments explaining the purpose of the different slots.
https://github.com/qtproject/qt-creator/blob/9926fc2ab12ccaa02b7f03b416c54cd58ef30b31/src/plugins/coreplugin/dialogs/ioptionspage.cpp
Basically for Qt Creators options page, it has an interface that is used by several different sub modules.
https://github.com/qtproject/qt-creator/blob/9926fc2ab12ccaa02b7f03b416c54cd58ef30b31/src/plugins/coreplugin/dialogs/ioptionspage.h
#ifndef IOPTIONSPAGE_H
#define IOPTIONSPAGE_H
#include <coreplugin/id.h>
#include <QIcon>
#include <QObject>
#include <QStringList>
namespace Core {
class CORE_EXPORT IOptionsPage : public QObject
{
Q_OBJECT
public:
IOptionsPage(QObject *parent = 0);
virtual ~IOptionsPage();
Id id() const { return m_id; }
QString displayName() const { return m_displayName; }
Id category() const { return m_category; }
QString displayCategory() const { return m_displayCategory; }
QIcon categoryIcon() const { return QIcon(m_categoryIcon); }
virtual bool matches(const QString &searchKeyWord) const;
virtual QWidget *widget() = 0;
virtual void apply() = 0;
virtual void finish() = 0;
protected:
void setId(Id id) { m_id = id; }
void setDisplayName(const QString &displayName) { m_displayName = displayName; }
void setCategory(Id category) { m_category = category; }
void setDisplayCategory(const QString &displayCategory) { m_displayCategory = displayCategory; }
void setCategoryIcon(const QString &categoryIcon) { m_categoryIcon = categoryIcon; }
Id m_id;
Id m_category;
QString m_displayName;
QString m_displayCategory;
QString m_categoryIcon;
mutable bool m_keywordsInitialized;
mutable QStringList m_keywords;
};
/*
Alternative way for providing option pages instead of adding IOptionsPage
objects into the plugin manager pool. Should only be used if creation of the
actual option pages is not possible or too expensive at Qt Creator startup.
(Like the designer integration, which needs to initialize designer plugins
before the options pages get available.)
*/
class CORE_EXPORT IOptionsPageProvider : public QObject
{
Q_OBJECT
public:
IOptionsPageProvider(QObject *parent = 0) : QObject(parent) {}
Id category() const { return m_category; }
QString displayCategory() const { return m_displayCategory; }
QIcon categoryIcon() const { return QIcon(m_categoryIcon); }
virtual QList<IOptionsPage *> pages() const = 0;
virtual bool matches(const QString & /* searchKeyWord*/) const = 0;
protected:
void setCategory(Id category) { m_category = category; }
void setDisplayCategory(const QString &displayCategory) { m_displayCategory = displayCategory; }
void setCategoryIcon(const QString &categoryIcon) { m_categoryIcon = categoryIcon; }
Id m_category;
QString m_displayCategory;
QString m_categoryIcon;
};
} // namespace Core
#endif // IOPTIONSPAGE_H
The search box uses an index the all the titles/labels of the children of each options page that gets added.
bool Core::IOptionsPage::matches(const QString &searchKeyWord) const
{
if (!m_keywordsInitialized) {
IOptionsPage *that = const_cast<IOptionsPage *>(this);
QWidget *widget = that->widget();
if (!widget)
return false;
// find common subwidgets
foreach (const QLabel *label, widget->findChildren<QLabel *>())
m_keywords << label->text();
foreach (const QCheckBox *checkbox, widget->findChildren<QCheckBox *>())
m_keywords << checkbox->text();
foreach (const QPushButton *pushButton, widget->findChildren<QPushButton *>())
m_keywords << pushButton->text();
foreach (const QGroupBox *groupBox, widget->findChildren<QGroupBox *>())
m_keywords << groupBox->title();
// clean up accelerators
QMutableStringListIterator it(m_keywords);
while (it.hasNext())
it.next().remove(QLatin1Char('&'));
m_keywordsInitialized = true;
}
foreach (const QString &keyword, m_keywords)
if (keyword.contains(searchKeyWord, Qt::CaseInsensitive))
return true;
return false;
}
Finding the rest of the components of the original dialog may take some time, but it is doable.
Included Example
When in Qt Creator > Welcome (tab) > Examples, the best one for a complex settings dialog is probably:
Tab Dialog Example
http://doc.qt.io/qt-5/qtwidgets-dialogs-tabdialog-example.html
Persistent Settings
QSettings is probably the best bet for storing settings. Other options include XML, and JSON. Qt 5 has a great implementation of JSON.
Hope that helps.

Get notification when contacts are added/deleted/updated

On BlackBerry 10, how can my Qt application be notified when a contact is added, deleted, or updated? Is there a contact API?
QContactManager has these 3 signals:
void QContactManager::contactsAdded ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsChanged ( const QList<QContactLocalId> & contactIds );
void QContactManager::contactsRemoved ( const QList<QContactLocalId> & contactIds );
Connect your slots to them in order to receive the notifications.
---hpp file------
API : bb::pim::contacts
bb/pim/contacts/ContactService
bb/pim/contacts/Contact
bb/pim/contacts/ContactAttributeBuilder
bb/pim/contacts/ContactBuilder..
see folwing example
#include <bb/pim/contacts/ContactService>
#include <QtCore/QObject>
class ContactEditor : public QObject
{
Q_OBJECT
// The data properties of the contact that is created or updated
Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY firstNameChanged)
Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY lastNameChanged)
Q_PROPERTY(QDateTime birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
// Defines whether the editor is in 'create' or 'edit' mode
Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
Q_ENUMS(Mode)
public:
/**
* Describes the mode of the contact editor.
* The mode information are used to adapt the behavior of the editor and
* provide hints to the UI.
*/
enum Mode {
CreateMode,
EditMode
};
ContactEditor(bb::pim::contacts::ContactService *service, QObject *parent = 0);
void setMode(Mode mode);
Mode mode() const;
public Q_SLOTS:
/**
* Loads the contact with the given ID.
*/
void loadContact(bb::pim::contacts::ContactId contactId);
/**
* Save the currently loaded contact if in 'edit' mode or creates a new one
* if in 'create' mode.
*/
void saveContact();
/**
* Resets all fields of the contact editor.
*/
void reset();
Q_SIGNALS:
// The change notification signals of the properties
void firstNameChanged();
void lastNameChanged();
void birthdayChanged();
void emailChanged();
void modeChanged();
private:
// The accessor methods of the properties
void setFirstName(const QString &firstName);
QString firstName() const;
void setLastName(const QString &lastName);
QString lastName() const;
void setBirthday(const QDateTime &birthday);
QDateTime birthday() const;
void setEmail(const QString &email);
QString email() const;
// The central object to access the contact service
bb::pim::contacts::ContactService *m_contactService;
// The ID of the currently loaded contact (if in 'edit' mode)
bb::pim::contacts::ContactId m_contactId;
// The property values
QString m_firstName;
QString m_lastName;
QDateTime m_birthday;
QString m_email;
Mode m_mode;
};
//! [0]
#endif
-----cpp file -----
using namespace bb::pim::contacts;
//! [0]
/**
* A helper method to update a single attribute on a Contact object.
* It first deletes the old attribute (if it exists) and adds the attribute with the
* new value afterwards.
*/
template <typename T>
static void updateContactAttribute(ContactBuilder &builder, const Contact &contact,
AttributeKind::Type kind, AttributeSubKind::Type subKind,
const T &value)
{
// Delete previous instance of the attribute
QList<ContactAttribute> attributes = contact.filteredAttributes(kind);
foreach (const ContactAttribute &attribute, attributes) {
if (attribute.subKind() == subKind)
builder.deleteAttribute(attribute);
}
// Add new instance of the attribute with new value
builder.addAttribute(ContactAttributeBuilder()
.setKind(kind)
.setSubKind(subKind)
.setValue(value));
}
//! [0]
//! [1]
ContactEditor::ContactEditor(ContactService *service, QObject *parent)
: QObject(parent)
, m_contactService(service)
, m_contactId(-1)
, m_birthday(QDateTime::currentDateTime())
, m_mode(CreateMode)
{
}
//! [1]
//! [2]
void ContactEditor::loadContact(ContactId contactId)
{
m_contactId = contactId;
// Load the contact from the persistent storage
const Contact contact = m_contactService->contactDetails(m_contactId);
// Update the properties with the data from the contact
m_firstName = contact.firstName();
m_lastName = contact.lastName();
m_birthday = QDateTime::currentDateTime();
const QList<ContactAttribute> dateAttributes = contact.filteredAttributes(AttributeKind::Date);
foreach (const ContactAttribute &dateAttribute, dateAttributes) {
if (dateAttribute.subKind() == AttributeSubKind::DateBirthday)
m_birthday = dateAttribute.valueAsDateTime();
}
m_email.clear();
const QList<ContactAttribute> emails = contact.emails();
if (!emails.isEmpty())
m_email = emails.first().value();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
//! [2]
//! [3]
void ContactEditor::saveContact()
{
if (m_mode == CreateMode) {
// Create a builder to assemble the new contact
ContactBuilder builder;
// Set the first name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameGiven)
.setValue(m_firstName));
// Set the last name
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Name)
.setSubKind(AttributeSubKind::NameSurname)
.setValue(m_lastName));
// Set the birthday
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Date)
.setSubKind(AttributeSubKind::DateBirthday)
.setValue(m_birthday));
// Set the email address
builder.addAttribute(ContactAttributeBuilder()
.setKind(AttributeKind::Email)
.setSubKind(AttributeSubKind::Other)
.setValue(m_email));
// Save the contact to persistent storage
m_contactService->createContact(builder, false);
} else if (m_mode == EditMode) {
// Load the contact from persistent storage
Contact contact = m_contactService->contactDetails(m_contactId);
if (contact.id()) {
// Create a builder to modify the contact
ContactBuilder builder = contact.edit();
// Update the single attributes
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameGiven, m_firstName);
updateContactAttribute<QString>(builder, contact, AttributeKind::Name, AttributeSubKind::NameSurname, m_lastName);
updateContactAttribute<QDateTime>(builder, contact, AttributeKind::Date, AttributeSubKind::DateBirthday, m_birthday);
updateContactAttribute<QString>(builder, contact, AttributeKind::Email, AttributeSubKind::Other, m_email);
// Save the updated contact back to persistent storage
m_contactService->updateContact(builder);
}
}
}
//! [3]
//! [4]
void ContactEditor::reset()
{
// Reset all properties
m_firstName.clear();
m_lastName.clear();
m_birthday = QDateTime::currentDateTime();
m_email.clear();
// Emit the change notifications
emit firstNameChanged();
emit lastNameChanged();
emit birthdayChanged();
emit emailChanged();
}
//! [4]
void ContactEditor::setFirstName(const QString &firstName)
{
if (m_firstName == firstName)
return;
m_firstName = firstName;
emit firstNameChanged();
}
QString ContactEditor::firstName() const
{
return m_firstName;
}
void ContactEditor::setLastName(const QString &lastName)
{
if (m_lastName == lastName)
return;
m_lastName = lastName;
emit lastNameChanged();
}
QString ContactEditor::lastName() const
{
return m_lastName;
}
void ContactEditor::setBirthday(const QDateTime &birthday)
{
if (m_birthday.date() == birthday.date())
return;
m_birthday = birthday;
emit birthdayChanged();
}
QDateTime ContactEditor::birthday() const
{
return m_birthday;
}
void ContactEditor::setEmail(const QString &email)
{
if (m_email == email)
return;
m_email = email;
emit emailChanged();
}
QString ContactEditor::email() const
{
return m_email;
}
void ContactEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
ContactEditor::Mode ContactEditor::mode() const
{
return m_mode;
}
use alert like this -->
alert(tr("contact saved"));
alert(tr("contact added"));
alert(tr("contact deleted"));
refer following sample
-----------qml--------------
Button {
horizontalAlignment: HorizontalAlignment.Center
text: qsTr("Update")
onClicked: {
_app.updateRecord(idUpdateTextField.text, firstNameUpdateTextField.text, lastNameUpdateTextField.text);
}
}
-----------------cpp file-------------------
bool App::updateRecord(const QString &customerID, const QString &firstName, const QString &lastName)
{
bool intConversionGood = false;
const int customerIDKey = customerID.toInt(&intConversionGood);
if (!intConversionGood) {
alert(tr("You must provide valid integer key."));
return false;
}
QSqlDatabase database = QSqlDatabase::database();
QSqlQuery query(database);
const QString sqlCommand = "UPDATE customers "
" SET firstName = :firstName, lastName = :lastName"
" WHERE customerID = :customerID";
query.prepare(sqlCommand);
query.bindValue(":firstName", firstName);
query.bindValue(":lastName", lastName);
query.bindValue(":customerID", customerIDKey);
bool updated = false;
if (query.exec()) {
if (query.numRowsAffected() > 0) {
alert(tr("Customer with id=%1 was updated.").arg(customerID));
updated = true;
} else {
alert(tr("Customer with id=%1 was not found.").arg(customerID));
}

How to refresh a QSqlTableModel while preserving the selection?

I am using a QSqlTableModel and QTableView to view an SQLite database table.
I would like to have the table auto refresh every second or so (it's not going to be a very large table - a couple of hundred rows). And i can do this - like so:
QTimer *updateInterval = new QTimer(this);
updateInterval->setInterval(1000);
updateInterval->start();
connect(updateInterval, SIGNAL(timeout()),this, SLOT(update_table()));
...
void MainWindow::update_table()
{
model->select(); //QSqlTableModel*
sqlTable->reset(); //QTableView*
}
But this removes any selection I have, so the selections only last for up to a second. This is annoying, as another pane in the GUI depends on what is selected. If nothing is selected, then it resets to an explanation splash page.
I then tried a somewhat hacky approach, which gets the selected row number, resets the table, and then selects that row. But this doesn't work either, as the selected row can move up or down based on additions to the table.
I know other classes have a dataChanged() signal, which would be ideal.
Do any of you know how I could have the table refresh to reflect changes to the database (from either command line usage, or other instances of the program) AND keep the current selection?
I know I could get data from the current selection, and then after the reset search for the same row and then reselect it, but this seems like a counter productive and bad solution to the problem.
EDIT: Current attempt at solution:
void MainWindow::update_table()
{
QList<QModelIndex> selection = sqlTable->selectionModel()->selection().indexes();
QList<int> selectedIDs;
bool somethingSelected = true;
for(QList<QModelIndex>::iterator i = selection.begin(); i != selection.end(); ++i){
int col = i->column();
QVariant data = i->data(Qt::DisplayRole);
if(col == 0) {
selectedIDs.append(data.toInt());
}
}
if(selectedIDs.empty()) somethingSelected = false;
model->select();
sqlTable->reset();
if(somethingSelected){
QList<int> selectedRows;
int rows = model->rowCount(QModelIndex());
for(int i = 0; i < rows; ++i){
sqlTable->selectRow(i);
if(selectedIDs.contains(sqlTable->selectionModel()->selection().indexes().first().data(Qt::DisplayRole).toInt())) selectedRows.append(i);
}
for(QList<int>::iterator i = selectedRows.begin(); i != selectedRows.end(); ++i){
sqlTable->selectRow(*i);
}
}
}
Okay so this more or less works now...
The real deal is the primary key on the result of your query. Qt's APIs offer a rather circuitous route from QSqlTableModel::primaryKey() to a list of columns. The result of primaryKey() is a QSqlRecord, and you can iterate over its field()s to see what they are. You can also look up all of the fields that comprise the query proper from QSqlTableModel::record(). You find the former in the latter to get a list of model columns that comprise the query.
If your query doesn't contain a primary key, you'll have to design one yourself and offer it using some protocol. For example, you can choose that if primaryKey().isEmpty() is true, the last column returned by the model is to be used as the primary key. It's up to you to figure out how to key the result of an arbitrary query.
The selected rows can then be indexed simply by their primary keys (a list of values of the cells that comprise the key -- a QVariantList). For this, you could use a custom selection model (QItemSelectionModel) if its design wasn't broken. The key methods, such as isRowSelected() aren't virtual, and you can't reimplement them :(.
Instead, you can use a proxy model that mimicks selection by providing a custom Qt::BackgroundRole for the data. Your model sits on top of the table model, and keeps a sorted list of selected keys. Each time the proxy model's data() is called, you get the row's key from underlying query model, then search for it in your sorted list. Finally, you return a custom background role if the item is selected. You'll have to write relevant comparison operator for QVariantList. If QItemSelectionModel was usable for this purpose, you could put this functionality into a reimplementation of isRowSelected().
The model is generic since you subscribe to a certain protocol for extracting the key from a query model: namely, using primaryKey().
Instead of using primary keys explicitly, you can also use persistent indices if the model supports them. Alas, until at lest Qt 5.3.2, QSqlTableModel does not preserve the persistent indices when the query is rerun. Thus, as soon as the view changes the sort order, the persistent indices become invalid.
Below is a fully worked out example of how one might implement such a beast:
#include <QApplication>
#include <QTableView>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QIdentityProxyModel>
#include <QSqlDatabase>
#include <QMap>
#include <QVBoxLayout>
#include <QPushButton>
// Lexicographic comparison for a variant list
bool operator<(const QVariantList &a, const QVariantList &b) {
int count = std::max(a.count(), b.count());
// For lexicographic comparison, null comes before all else
Q_ASSERT(QVariant() < QVariant::fromValue(-1));
for (int i = 0; i < count; ++i) {
auto aValue = i < a.count() ? a.value(i) : QVariant();
auto bValue = i < b.count() ? b.value(i) : QVariant();
if (aValue < bValue) return true;
}
return false;
}
class RowSelectionEmulatorProxy : public QIdentityProxyModel {
Q_OBJECT
Q_PROPERTY(QBrush selectedBrush READ selectedBrush WRITE setSelectedBrush)
QMap<QVariantList, QModelIndex> mutable m_selection;
QVector<int> m_roles;
QBrush m_selectedBrush;
bool m_ignoreReset;
class SqlTableModel : public QSqlTableModel {
public:
using QSqlTableModel::primaryValues;
};
SqlTableModel * source() const {
return static_cast<SqlTableModel*>(dynamic_cast<QSqlTableModel*>(sourceModel()));
}
QVariantList primaryValues(int row) const {
auto record = source()->primaryValues(row);
QVariantList values;
for (int i = 0; i < record.count(); ++i) values << record.field(i).value();
return values;
}
void notifyOfChanges(int row) {
emit dataChanged(index(row, 0), index(row, columnCount()-1), m_roles);
}
void notifyOfAllChanges(bool remove = false) {
auto it = m_selection.begin();
while (it != m_selection.end()) {
if (it->isValid()) notifyOfChanges(it->row());
if (remove) it = m_selection.erase(it); else ++it;
}
}
public:
RowSelectionEmulatorProxy(QObject* parent = 0) :
QIdentityProxyModel(parent), m_roles(QVector<int>() << Qt::BackgroundRole),
m_ignoreReset(false) {
connect(this, &QAbstractItemModel::modelReset, [this]{
if (! m_ignoreReset) {
m_selection.clear();
} else {
for (auto it = m_selection.begin(); it != m_selection.end(); ++it) {
*it = QModelIndex(); // invalidate the cached mapping
}
}
});
}
QBrush selectedBrush() const { return m_selectedBrush; }
void setSelectedBrush(const QBrush & brush) {
if (brush == m_selectedBrush) return;
m_selectedBrush = brush;
notifyOfAllChanges();
}
QList<int> selectedRows() const {
QList<int> result;
for (auto it = m_selection.begin(); it != m_selection.end(); ++it) {
if (it->isValid()) result << it->row();
}
return result;
}
bool isRowSelected(const QModelIndex &proxyIndex) const {
if (! source() || proxyIndex.row() >= rowCount()) return false;
auto primaryKey = primaryValues(proxyIndex.row());
return m_selection.contains(primaryKey);
}
Q_SLOT void selectRow(const QModelIndex &proxyIndex, bool selected = true) {
if (! source() || proxyIndex.row() >= rowCount()) return;
auto primaryKey = primaryValues(proxyIndex.row());
if (selected) {
m_selection.insert(primaryKey, proxyIndex);
} else {
m_selection.remove(primaryKey);
}
notifyOfChanges(proxyIndex.row());
}
Q_SLOT void toggleRowSelection(const QModelIndex &proxyIndex) {
selectRow(proxyIndex, !isRowSelected(proxyIndex));
}
Q_SLOT virtual void clearSelection() {
notifyOfAllChanges(true);
}
QVariant data(const QModelIndex &proxyIndex, int role) const Q_DECL_OVERRIDE {
QVariant value = QIdentityProxyModel::data(proxyIndex, role);
if (proxyIndex.row() < rowCount() && source()) {
auto primaryKey = primaryValues(proxyIndex.row());
auto it = m_selection.find(primaryKey);
if (it != m_selection.end()) {
// update the cache
if (! it->isValid()) *it = proxyIndex;
// return the background
if (role == Qt::BackgroundRole) return m_selectedBrush;
}
}
return value;
}
bool setData(const QModelIndex &, const QVariant &, int) Q_DECL_OVERRIDE {
return false;
}
void sort(int column, Qt::SortOrder order) Q_DECL_OVERRIDE {
m_ignoreReset = true;
QIdentityProxyModel::sort(column, order);
m_ignoreReset = false;
}
void setSourceModel(QAbstractItemModel * model) Q_DECL_OVERRIDE {
m_selection.clear();
QIdentityProxyModel::setSourceModel(model);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget w;
QVBoxLayout layout(&w);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (! db.open()) return 255;
QSqlQuery query(db);
query.exec("create table chaps (name, age, constraint pk primary key (name, age));");
query.exec("insert into chaps (name, age) values "
"('Bob', 20), ('Rob', 30), ('Sue', 25), ('Hob', 40);");
QSqlTableModel model(nullptr, db);
model.setTable("chaps");
RowSelectionEmulatorProxy proxy;
proxy.setSourceModel(&model);
proxy.setSelectedBrush(QBrush(Qt::yellow));
QTableView view;
view.setModel(&proxy);
view.setEditTriggers(QAbstractItemView::NoEditTriggers);
view.setSelectionMode(QAbstractItemView::NoSelection);
view.setSortingEnabled(true);
QObject::connect(&view, &QAbstractItemView::clicked, [&proxy](const QModelIndex & index){
proxy.toggleRowSelection(index);
});
QPushButton clearSelection("Clear Selection");
QObject::connect(&clearSelection, &QPushButton::clicked, [&proxy]{ proxy.clearSelection(); });
layout.addWidget(&view);
layout.addWidget(&clearSelection);
w.show();
app.exec();
}
#include "main.moc"