Results 1 to 5 of 5

Thread: db populates model based QTreeView, rowclicked populates QTextBrowser but...

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default db populates model based QTreeView, rowclicked populates QTextBrowser but...

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: db populates model based QTreeView, rowclicked populates QTextBrowser but...

    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.

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: db populates model based QTreeView, rowclicked populates QTextBrowser but...

    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:ialog *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

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: db populates model based QTreeView, rowclicked populates QTextBrowser but...

    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:
    Qt Code:
    1. void rowClicked(const QModelIndex& ...) {
    2. // E:\Bird\splash1\mainwindow.cpp:-1: In function 'void rowClicked(const QModelIndex&)':
    3. // E:\Bird\splash1\mainwindow.cpp:181: error: 'model' was not declared in this scope
    4. // E:\Bird\splash1\mainwindow.cpp:186: error: 'ui' was not declared in this scope
    5. // E:\Bird\splash1\mainwindow.cpp:181: warning: unused variable 'id' [-Wunused-variable]
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    then the issue is that rowClicked() is a free function and not part of the MainWindow class (i.e. void MainWindow::rowClicked()).

  5. The following user says thank you to ChrisW67 for this useful post:

    eutectoid (26th June 2013)

  6. #5
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: db populates model based QTreeView, rowclicked populates QTextBrowser but...

    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

Similar Threads

  1. Replies: 7
    Last Post: 8th February 2013, 23:26
  2. From item based widgets to model/view
    By timom in forum Qt Programming
    Replies: 2
    Last Post: 22nd September 2011, 07:35
  3. Qcombobox and model/item based question
    By yaguis in forum Newbie
    Replies: 5
    Last Post: 7th June 2010, 01:22
  4. QComboBox (model based) - provide an empty entry
    By antarctic in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 18:45
  5. Replies: 21
    Last Post: 5th January 2008, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.