Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Sort/filter model/view from database table!
QtWS25 Last Chance

Sort/filter model/view from database table!

Scheduled Pinned Locked Moved Qt Creator and other tools
12 Posts 3 Posters 9.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    freecamellia
    wrote on last edited by
    #3

    Thanks for your replay.

    The data() method return a QVariant value, not QString. And I need the second column col2 instead of the first. So some modifications are required as follow:

    @int r = i.row();
    QVariant v = proxyModel->data(proxyModel->index(r,2), Qt::DisplayRole);
    QString str = v.toString();
    ui->textEdit->setText(str);@

    but I have always the same problem with the filter, I don't find the solution and I am stopped now...

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #4

      This code works for me:

      create-qdndb.sql

      @
      CREATE TABLE qdntest (
      col1 varchar(100) not null,
      col2 varchar(100) not null
      );

      INSERT INTO qdntest VALUES( 'abc' ,'column 2 of abc');
      INSERT INTO qdntest VALUES( 'aboc' ,'column 2 of aboc');
      INSERT INTO qdntest VALUES( 'abzee' ,'column 2 of abzee');
      INSERT INTO qdntest VALUES( 'qdn' ,'column 2 of qdn');
      INSERT INTO qdntest VALUES( 'test' ,'column 2 of test');
      INSERT INTO qdntest VALUES( 'what' ,'column 2 of what');
      @

      mainwindow.h

      @

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QtGui/QDialog>

      class QListView;
      class QLineEdit;
      class QTextEdit;
      class QSqlTableModel;
      class QSortFilterProxyModel;
      class QModelIndex;

      class MainWindow : public QDialog
      {
      Q_OBJECT

      public:
      MainWindow(QWidget *parent = 0);
      ~MainWindow();

      protected slots:
      void filterChanged(const QString &filter);
      void itemClicked(const QModelIndex &index);

      private:
      QListView *listView;
      QLineEdit *lineEdit;
      QTextEdit *textEdit;
      QSqlTableModel *tableModel;
      QSortFilterProxyModel *proxyModel;
      };

      #endif // MAINWINDOW_H
      @

      mainwindow.cpp

      @

      #include "mainwindow.h"

      #include <QListView>
      #include <QLineEdit>
      #include <QTextEdit>
      #include <QSqlTableModel>
      #include <QSortFilterProxyModel>
      #include <QVBoxLayout>
      #include <QSqlDatabase>

      MainWindow::MainWindow(QWidget *parent)
      : QDialog(parent)
      {
      QVBoxLayout *layout = new QVBoxLayout(this);

      lineEdit = new QLineEdit;
      layout->addWidget(lineEdit);
      
      listView = new QListView;
      layout->addWidget(listView);
      
      textEdit = new QTextEdit;
      layout->addWidget(textEdit);
      
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("/Users/volker/qdn.db");
      db.open();
      
      tableModel = new QSqlTableModel(this, db);
      tableModel->setTable("qdntest");
      tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
      tableModel->sort(0, Qt::AscendingOrder);
      tableModel->select();
      
      proxyModel = new QSortFilterProxyModel;
      proxyModel->setSourceModel(tableModel);
      
      listView->setModel(proxyModel);
      listView->setModelColumn(0);
      
      connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged(QString)));
      connect(listView, SIGNAL(clicked(QModelIndex)), this, SLOT(itemClicked(QModelIndex)));
      

      }

      MainWindow::~MainWindow()
      {
      }

      void MainWindow::filterChanged(const QString &filter)
      {
      proxyModel->setFilterRegExp(QString("^%1").arg(filter));
      }

      void MainWindow::itemClicked(const QModelIndex &index)
      {
      QString text = proxyModel->data(proxyModel->index(index.row(), 1), Qt::DisplayRole).toString();
      textEdit->setPlainText(text);
      }
      @

      main.cpp

      @
      #include <QtGui/QApplication>
      #include "mainwindow.h"

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();

      return a.exec&#40;&#41;;
      

      }
      @

      qdntest.pro

      @

      #-------------------------------------------------

      Project created by QtCreator 2011-12-04T22:06:13

      #-------------------------------------------------

      QT += core gui sql

      TARGET = SqlProxyModelTest
      TEMPLATE = app

      SOURCES += main.cpp
      mainwindow.cpp

      HEADERS += mainwindow.h
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • F Offline
        F Offline
        freecamellia
        wrote on last edited by
        #5

        Sorry, I tested it but the filter always don't work! when I type something in the lineEdit, the listView becomes empty.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #6

          So, that seems reasonable to me if no item in the list matches the filter text. What is the actual problem?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #7

            So what didd you type in? Of course, there will only be items remaining, if you start your search with one of the letters a, q, t or w.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • F Offline
              F Offline
              freecamellia
              wrote on last edited by
              #8

              Yes, even if I type a, q, t or w, the listView becomes empty again.
              It works for you?!

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #9

                [quote author="freecamellia" date="1323086553"]Yes, even if I type a, q, t or w, the listView becomes empty again.
                It works for you?![/quote]

                Of course:

                [quote author="Volker" date="1323033824"]This code works for me:[/quote]

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  freecamellia
                  wrote on last edited by
                  #10

                  Thank you for your helps during all this time.
                  Regards.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    freecamellia
                    wrote on last edited by
                    #11

                    [quote author="Andre" date="1323085112"]So, that seems reasonable to me if no item in the list matches the filter text. What is the actual problem?[/quote]

                    [quote author="freecamellia" date="1322956485"]when I type something in the lineEdit, the listView displays nothing.
                    [/quote]

                    This is my problem.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #12

                      That only means that your filter filters out everything: no row matches whatever the filter was that was created based on what was typed in the line edit.

                      I have no way of checking how you implemented that, but based on the implementation shown in this topic:
                      @
                      void MainWindow::filterChanged(const QString &filter)
                      {
                      proxyModel->setFilterRegExp(QString("^%1").arg(filter));
                      }
                      @
                      the filter should match any item that starts with whatever you typed in the line edit.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved