Hi to all,
This is my first post and I'm trying to learn QT and understand the model/view stuff. Apologies for the long code.
I've set up a Dialog project and this is my code
In dialog.h
private slots:
void on_pushButtonA_clicked();
private:
Ui::Dialog *ui;
=========================
In dialog.cpp
void Dialog::on_pushButtonA_clicked()
{
ui->textBrowser->clear();
ui->pushButtonC->setFocus();
model->setQuery("SELECT id, mot, tag, def FROM aTable");
ui->treeView->setModel(model);
ui->treeView->hideColumn(0);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
ui->treeView->scrollToTop();
ui
->lblWords
->setText
(QString("0"));
QString thisquery
= "SELECT COUNT (*) FROM aTable";
int numRecs = 0;
if(!query.exec(thisquery) || !query.next() == true){
QMessageBox::critical(0,
"Query COUNT(*) failed on aTable...", err.
text());
}
else{
numRecs = query.value(0).toInt();
}
ui
->lblWords
->setText
(QString::number(numRecs
));
}
{
int row = index.row();
QString id
= model
->record
(row
).
value("id").
toString();
QString mot
= model
->record
(row
).
value("mot").
toString();
QString tag
= model
->record
(row
).
value("tag").
toString();
QString def
= model
->record
(row
).
value("def").
toString();
ui->textBrowser->clear();
ui->textBrowser->append("<font color='blue'>" + mot + "</font>");
ui->textBrowser->append(mot);
ui->textBrowser->append("");
ui->textBrowser->append(" " + tag);
ui->textBrowser->append("");
ui->textBrowser->append(def);
cursor.setPosition(0);
ui->textBrowser->setTextCursor(cursor);
}
private slots:
void on_pushButtonA_clicked();
void rowClicked(const QModelIndex &index);
private:
Ui::Dialog *ui;
QSqlQueryModel *model;
QSqlDatabase db;
QItemSelectionModel *selectModel;
=========================
In dialog.cpp
void Dialog::on_pushButtonA_clicked()
{
ui->textBrowser->clear();
ui->pushButtonC->setFocus();
this->model = new QSqlQueryModel();
model->setQuery("SELECT id, mot, tag, def FROM aTable");
ui->treeView->setModel(model);
ui->treeView->hideColumn(0);
ui->treeView->hideColumn(2);
ui->treeView->hideColumn(3);
ui->treeView->scrollToTop();
connect(ui->treeView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(rowClicked(const QModelIndex &)));
ui->lblWords->setText(QString("0"));
QString thisquery = "SELECT COUNT (*) FROM aTable";
QSqlQuery query;
int numRecs = 0;
if(!query.exec(thisquery) || !query.next() == true){
QSqlError err = query.lastError();
QMessageBox::critical(0, "Query COUNT(*) failed on aTable...", err.text());
}
else{
numRecs = query.value(0).toInt();
}
ui->lblWords->setText(QString::number(numRecs));
}
void Dialog::rowClicked(const QModelIndex &index)
{
int row = index.row();
QString id = model->record(row).value("id").toString();
QString mot = model->record(row).value("mot").toString();
QString tag = model->record(row).value("tag").toString();
QString def = model->record(row).value("def").toString();
ui->textBrowser->clear();
ui->textBrowser->append("<font color='blue'>" + mot + "</font>");
ui->textBrowser->append(mot);
ui->textBrowser->append("");
ui->textBrowser->append(" " + tag);
ui->textBrowser->append("");
ui->textBrowser->append(def);
QTextCursor cursor = ui->textBrowser->textCursor();
cursor.setPosition(0);
ui->textBrowser->setTextCursor(cursor);
}
To copy to clipboard, switch view to plain text mode
This works but when I try the same with a QMainWindow project changing the following:
Dialog version - .h file
void on_pushButtonA_clicked();
void on_actionA_triggered();
Dialog version - .cpp file
void Dialog::on_pushButtonA_clicked()
etc
etc
void MainWindow::on_actionA_triggered()
etc
etc
Dialog version - .h file
void on_pushButtonA_clicked();
void rowClicked(const QModelIndex &index);
QMainWindow version .h file
void on_actionA_triggered();
void rowClicked(const QModelIndex &index);
Dialog version - .cpp file
void Dialog::on_pushButtonA_clicked()
etc
void Dialog::rowClicked(const QModelIndex &index)
etc
QMainWindow version .cpp file
void MainWindow::on_actionA_triggered()
etc
void rowClicked(const QModelIndex &index)
etc
To copy to clipboard, switch view to plain text mode
I get these errors
E:\Bird\splash1\mainwindow.cpp:-1: In function 'void rowClicked(const QModelIndex&)':
E:\Bird\splash1\mainwindow.cpp:181: error: 'model' was not declared in this scope
E:\Bird\splash1\mainwindow.cpp:186: error: 'ui' was not declared in this scope
E:\Bird\splash1\mainwindow.cpp:181: warning: unused variable 'id' [-Wunused-variable]
Again, apologies for the long code and thanks in advance for any advice offered
Bookmarks