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

Qt Code:
  1. private slots:
  2. void on_pushButtonA_clicked();
  3. void rowClicked(const QModelIndex &index);
  4.  
  5. private:
  6. Ui::Dialog *ui;
  7. QItemSelectionModel *selectModel;
  8.  
  9. =========================
  10.  
  11. In dialog.cpp
  12. void Dialog::on_pushButtonA_clicked()
  13. {
  14. ui->textBrowser->clear();
  15. ui->pushButtonC->setFocus();
  16. this->model = new QSqlQueryModel();
  17. model->setQuery("SELECT id, mot, tag, def FROM aTable");
  18. ui->treeView->setModel(model);
  19.  
  20. ui->treeView->hideColumn(0);
  21. ui->treeView->hideColumn(2);
  22. ui->treeView->hideColumn(3);
  23.  
  24. ui->treeView->scrollToTop();
  25.  
  26. connect(ui->treeView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(rowClicked(const QModelIndex &)));
  27.  
  28. ui->lblWords->setText(QString("0"));
  29. QString thisquery = "SELECT COUNT (*) FROM aTable";
  30. QSqlQuery query;
  31. int numRecs = 0;
  32. if(!query.exec(thisquery) || !query.next() == true){
  33. QSqlError err = query.lastError();
  34. QMessageBox::critical(0, "Query COUNT(*) failed on aTable...", err.text());
  35. }
  36. else{
  37. numRecs = query.value(0).toInt();
  38. }
  39. ui->lblWords->setText(QString::number(numRecs));
  40. }
  41.  
  42.  
  43.  
  44.  
  45. void Dialog::rowClicked(const QModelIndex &index)
  46. {
  47. int row = index.row();
  48. QString id = model->record(row).value("id").toString();
  49. QString mot = model->record(row).value("mot").toString();
  50. QString tag = model->record(row).value("tag").toString();
  51. QString def = model->record(row).value("def").toString();
  52.  
  53. ui->textBrowser->clear();
  54. ui->textBrowser->append("<font color='blue'>" + mot + "</font>");
  55. ui->textBrowser->append(mot);
  56. ui->textBrowser->append("");
  57. ui->textBrowser->append(" " + tag);
  58. ui->textBrowser->append("");
  59. ui->textBrowser->append(def);
  60.  
  61. QTextCursor cursor = ui->textBrowser->textCursor();
  62. cursor.setPosition(0);
  63. ui->textBrowser->setTextCursor(cursor);
  64. }
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:

Qt Code:
  1. Dialog version - .h file
  2. void on_pushButtonA_clicked();
  3. void rowClicked(const QModelIndex &index);
  4.  
  5. QMainWindow version .h file
  6. void on_actionA_triggered();
  7. void rowClicked(const QModelIndex &index);
  8.  
  9.  
  10. Dialog version - .cpp file
  11. void Dialog::on_pushButtonA_clicked()
  12. etc
  13.  
  14. void Dialog::rowClicked(const QModelIndex &index)
  15. etc
  16.  
  17.  
  18. QMainWindow version .cpp file
  19. void MainWindow::on_actionA_triggered()
  20. etc
  21.  
  22. void rowClicked(const QModelIndex &index)
  23. 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