Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting QTableview items alphabetically!

Sorting QTableview items alphabetically!

Scheduled Pinned Locked Moved Solved General and Desktop
tableviewcolumnitemssortalphabetical
6 Posts 3 Posters 4.4k 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.
  • K Offline
    K Offline
    Kushan
    wrote on 20 Nov 2017, 09:38 last edited by VRonin
    #1

    In my QT C++ application when load button is clicked a database table column is displayed in a table view! I need to arrange the words when they are loaded in alphabetical order! How can I modify my code?

    Dialog.h

    ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    class QSqlQueryModel;
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
    Q_OBJECT
    
    public:
    explicit Dialog(QWidget *parent = 0);
    
    ~Dialog();
    
    private slots:
    void on_searchMessages_clicked();
    void on_pushButton_clicked();
    
    private:
    Ui::Dialog *ui;
    QSqlQueryModel modal;
    };
    

    Dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QSqlQuery>
    #include <QSqlQueryModel>
    #include "mainwindow.h"
    #include <QSqlDatabase>
    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog),
    modal(new QSqlQueryModel(this)),
    
    {
    ui->setupUi(this);
    
    }
    Dialog::~Dialog()
    {
    delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
    {
    if(QSqlDatabase::contains("MyConnection")){
    
            QSqlQuery qry(QSqlDatabase::database("MyConnection"));
       
            qry.prepare("Select UserName from User");
          
    
            if(!qry.exec()) return;
            modal->setQuery(qry);
          ui->tableView->resizeColumnsToContents();
        }
          QSqlDatabase::database("myconnection").close();
    }
    
    }
    
    }
    
    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 20 Nov 2017, 09:50 last edited by
      #2

      Did you get a chance to look at setSortingEnabled() and setSortColumn(..) APIs of QTableView. By default sorting is false. You can work with above APIs to achieve your requirement.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      K 1 Reply Last reply 20 Nov 2017, 09:59
      1
      • D dheerendra
        20 Nov 2017, 09:50

        Did you get a chance to look at setSortingEnabled() and setSortColumn(..) APIs of QTableView. By default sorting is false. You can work with above APIs to achieve your requirement.

        K Offline
        K Offline
        Kushan
        wrote on 20 Nov 2017, 09:59 last edited by
        #3

        @dheerendra I did ui->tableView->setSortingEnabled(true); yet the previous result is displayed

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 20 Nov 2017, 10:02 last edited by
          #4

          What is previous result ? I did not understand. Can u give more details ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • V Offline
            V Offline
            VRonin
            wrote on 20 Nov 2017, 10:13 last edited by VRonin
            #5

            two options:

            • put a QSortFilterProxyModel between your model and your view then call ui->tableView->sort(0)
            • change the query to qry.prepare("Select UserName from User order by UserName");

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            K 1 Reply Last reply 20 Nov 2017, 10:30
            1
            • V VRonin
              20 Nov 2017, 10:13

              two options:

              • put a QSortFilterProxyModel between your model and your view then call ui->tableView->sort(0)
              • change the query to qry.prepare("Select UserName from User order by UserName");
              K Offline
              K Offline
              Kushan
              wrote on 20 Nov 2017, 10:30 last edited by
              #6

              @VRonin Thanx mate

              1 Reply Last reply
              0

              2/6

              20 Nov 2017, 09:50

              topic:navigator.unread, 4
              • Login

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