PDA

View Full Version : db populates model based QTreeView, rowclicked populates QTextBrowser but...



eutectoid
24th June 2013, 08:52
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();
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);
}


This works but when I try the same with a QMainWindow project changing the following:


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

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

ChrisW67
24th June 2013, 21:38
The error messages mean exactly what they say, neither model nor ui is the name of a variable that is in scope at the point you try to use them. This is a C++ issue and not Qt. The equivalent of lines 6 and 7 in your original listing is missing from your seconds attempt.

eutectoid
25th June 2013, 13:00
Hi,
Thanks for the prompt reply but I've looked at both .h files and they both include lines 6 & 7


Dialog version - .h file

private slots:
void on_pushButtonA_clicked();
void rowClicked(const QModelIndex &index);

private:
Ui::Dialog *ui;
QSqlQueryModel *model;
QSqlDatabase db;
QItemSelectionModel *selectModel;

QMainWindow version .h file

private slots:
void on_actionA_triggered();
void rowClicked(const QModelIndex &index);

private:
Ui::MainWindow *ui;
QSqlQueryModel *model;
QSqlDatabase db;
QItemSelectionModel *selectModel;
QSortFilterProxyModel *proxy;

QLabel *statlabel;
QLabel *statrecs;

Is there anything else you could suggest I try.
Thanks in advance

ChrisW67
25th June 2013, 21:31
I suggest you read the error messages again. The compiler is not issuing them out of spite: ui and model are not valid where you are trying to use them.

If your code actually looks like:


void rowClicked(const QModelIndex& ...) {
// 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]
...
}

then the issue is that rowClicked() is a free function and not part of the MainWindow class (i.e. void MainWindow::rowClicked()).

eutectoid
26th June 2013, 08:06
Hi,
Thank you for your time and suggestions.
I did read the error messages and googled as to what they meant but as a newbie, I did not understand.
This part of the program works now and I won't make this mistake a second time.
Thanks once again for your input