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 a tree view. Qt5 vs Qt4 issue.
Forum Updated to NodeBB v4.3 + New Features

Sorting a tree view. Qt5 vs Qt4 issue.

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.4k Views 1 Watching
  • 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.
  • P phoenixa1

    So far I've not ran into too many differences between Qt5 and 4. The ones I have encountered have just been renamed functions. My problem now is the code compiles on both Qt5 and 4 but only works as expected on Qt5.

    I'm using a QTreeView and a QStandardItemModel with a QSortFilterProxyModel sandwiched between the two.

    In Qt5, I only need one line to sort the tee:

    proxy_model_m.sort(0, Qt::AscendingOrder);
    

    This line only occurs once. The tree is then kept sorted as the data in the underlying QStandardItemModel changes.

    The following snippet is from http://doc.qt.io/qt-4.8/qsortfilterproxymodel.html

    "An alternative approach to sorting is to disable sorting on the view and to impose a certain order to the user. This is done by explicitly calling sort() with the desired column and order as arguments on the QSortFilterProxyModel"

    Followed by the example code:

    proxyModel->sort(2, Qt::AscendingOrder);
    

    The specific versions of Qt are 4.8.7 and 5.7.1.

    Here's the actual code from my test program.

    mainwindow.cpp

    #include "mainwindow.hpp"
    #include "ui_mainwindow.h"
    
    #include "priv.hpp"
    
    #include <iostream>
    #include <thread>
    
    void status_update_thread(MainWindow *parent, bool *halt)
    {
    	signaler sig;
    	parent->connect(&sig, SIGNAL(update_signal()));
    
    	while(!(*halt)) {
    		std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    		sig.update();
    	}
    	std::cout << "Exiting" << std::endl;
    }
    
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow), halt_m(new bool(false)),
    model_m(100, 1, this), proxy_model_m(this)
    {
    	t1_m = new std::thread(status_update_thread, this, halt_m);
        ui->setupUi(this);
    
    	proxy_model_m.setSourceModel(&model_m);
    	ui->treeView->setModel(&proxy_model_m);
    
    	model_m.setRowCount(0);
    	model_m.setHorizontalHeaderLabels(QStringList({"Tree Info"}));
    	proxy_model_m.sort(0, Qt::AscendingOrder);
    }
    
    MainWindow::~MainWindow()
    {
    	kill_thread();
    	t1_m->join();
    	delete halt_m;
        delete ui;
    }
    
    void MainWindow::connect(QObject *obj, const char *sig)
    {
    	QObject::connect(obj, sig, this, SLOT(update_view()));
    }
    
    void MainWindow::kill_thread()
    {
    	std::cout << "Killing thread" << std::endl;
    	*halt_m = true;
    }
    
    void MainWindow::update_view()
    {
    	std::cout << "Update view" << std::endl;
    	for (int i = 0; i < 3; ++i) {
    		if (i == model_m.rowCount())
    			model_m.appendRow(new QStandardItem(QString("%1%2").arg(rand() % 100, 2, 10, QChar('0')).arg(QChar(65 + i))));
    		else
    			model_m.item(i)->setData(QString("%1%2").arg(rand() % 100, 2, 10, QChar('0')).arg(QChar(65 + i)), Qt::DisplayRole);
    		for (int j = 0; j < 3; ++j) {
    			if (j == model_m.item(i)->rowCount())
    				model_m.item(i)->appendRow(new QStandardItem(QString("%1%2").arg(rand() % 100, 2, 10, QChar('0')).arg(QChar(65 + j))));
    			else
    				model_m.item(i)->child(j)->setData(QString("%1%2").arg(rand() % 100, 2, 10, QChar('0')).arg(QChar(65 + j)), Qt::DisplayRole);
    		}
    	}
    }
    

    mainwindow.hpp

    #ifndef MAINWINDOW_HPP
    #define MAINWINDOW_HPP
    
    #include <QMainWindow>
    #include <QStandardItemModel>
    #include <QSortFilterProxyModel>
    
    namespace std {
    class thread;
    }
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    	void connect(QObject *obj, const char *sig);
    
    private:
    	void kill_thread();
    
    	Ui::MainWindow *ui;
    
    	bool *halt_m;
    	std::thread *t1_m;
    
    	QStandardItemModel model_m;
    	QSortFilterProxyModel proxy_model_m;
    
    private Q_SLOTS:
    	void update_view();
    };
    
    #endif // MAINWINDOW_HPP
    

    priv.hpp

    #ifndef PRIV_HPP
    #define PRIV_HPP
    
    #include <QObject>
    
    class signaler : public QObject {
    	Q_OBJECT
    
    public:
    	void update() {
    		update_signal();
    	}
    
    signals:
    	void update_signal();
    };
    
    #endif // PRIV_HPP
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>900</width>
        <height>608</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="0">
         <widget class="QTreeView" name="treeView"/>
        </item>
       </layout>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

    Note:The project file requires QMAKE_CXXFLAGS += -std=c++11 be appended.

    The items in the model get lettered in order. The numbers in front of the letters are random. If the sort works, the numbers should be in order and the letters may get shuffled around. Otherwise, the letters stay in order and the numbers likely wont be. The std::thread is used to simulate the item model being updated from an outside source.

    Thanks!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @phoenixa1 said in Sorting a tree view. Qt5 vs Qt4 issue.:

    QMAKE_CXXFLAGS += -std=c++11

    You should use

    CONFIG += c++11
    

    instead

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • P Offline
      P Offline
      phoenixa1
      wrote on last edited by
      #3

      Noted, but doing that breaks the project using Qt4.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        In what way ? Qt 4 should also support C++11

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • P Offline
          P Offline
          phoenixa1
          wrote on last edited by
          #5

          Qt4 does support C++11. To clarify, it's that appending "CONFIG += c++11" doesn't work when I try it with Qt4, but appending "QMAKE_CXXFLAGS += -std=c++11" does.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            Can you give more details about what "doesn't work" means in that case ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7
              • 99.999% convinced you have a race condition on halt_m (should be std::atomic_bool to work correctly).
              • I still don't understand what the problem is. What is happening in Qt5 that is not happening in Qt4?

              "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

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phoenixa1
                wrote on last edited by
                #8

                Yes, std::atomic_bool would have been a better choice for halt_m. I'll be sure to utilize it in the future, but that's not the issue here.

                I see now that I did a poor job of stating my actual problem, so here it is.

                I'm concerned with whether or not the QTreeView is being sorted.
                When build using Qt5, the tree gets sorted. Everything is fine.
                When build in Qt4, the tree doesn't get sorted. I don't know why.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  phoenixa1
                  wrote on last edited by
                  #9

                  Sorry, missed your response SGaist.

                  By "doesn't work" I mean that if I append "CONFIG += c++11" instead of "QMAKE_CXXFLAGS += -std=c++11" to the project file, then (in Qt4) it will fail to build (complaining that insert C++11 feature here requires the -std=c++11 flag) as though I hadn't appended anything at all.

                  In addition, I didn't have to append anything until I tried to build it in Qt4. Building in Qt5 (which is what I started in) required no additional modification to the project file.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    Might be a silly question but did you re-run qmake after adding CONFIG += c++11 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    P 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Might be a silly question but did you re-run qmake after adding CONFIG += c++11 ?

                      P Offline
                      P Offline
                      phoenixa1
                      wrote on last edited by
                      #11

                      @SGaist < Hey, I did this right!
                      Yes. Just to be absolutely sure, I closed Qt Creator, I wiped out the build folder and all *.pro.* (*.pro.user etc...) files, and changed QMAKE_CXXFLAGS += -std=c++11 to CONFIG += c++11 at the end of the project file with some text editor outside of Qt Creator.

                      The following is the result after freshly loading qt_proxy_model_test.pro into Qt Creator and trying to build.

                      This is the actual compile output.

                      15:46:53: Running steps for project qt_proxy_model_test...
                      15:46:53: Starting: "/usr/lib64/qt4/bin/qmake" /home/system/git/qt_proxy_model_test/qt_proxy_model_test.pro -r -spec linux-g++ CONFIG+=debug
                      15:46:53: The process "/usr/lib64/qt4/bin/qmake" exited normally.
                      15:46:53: Starting: "/usr/bin/make" 
                      /usr/lib64/qt4/bin/uic ../qt_proxy_model_test/mainwindow.ui -o ui_mainwindow.h
                      g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_DEPRECATED_WARNINGS -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../qt_proxy_model_test -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I../qt_proxy_model_test -I. -o main.o ../qt_proxy_model_test/main.cpp
                      g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_DEPRECATED_WARNINGS -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../qt_proxy_model_test -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I../qt_proxy_model_test -I. -o mainwindow.o ../qt_proxy_model_test/mainwindow.cpp
                      In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.5/include/g++-v4/thread:35:0,
                                       from ../qt_proxy_model_test/mainwindow.cpp:7:
                      /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.5/include/g++-v4/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
                       #error This file requires compiler and library support for the \
                        ^
                      ../qt_proxy_model_test/mainwindow.cpp: In function ‘void status_update_thread(MainWindow*, bool*)’:
                      ../qt_proxy_model_test/mainwindow.cpp:15:8: error: ‘std::this_thread’ has not been declared
                         std::this_thread::sleep_for(std::chrono::milliseconds(2000));
                              ^
                      ../qt_proxy_model_test/mainwindow.cpp:15:36: error: ‘std::chrono’ has not been declared
                         std::this_thread::sleep_for(std::chrono::milliseconds(2000));
                                                          ^
                      ../qt_proxy_model_test/mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
                      ../qt_proxy_model_test/mainwindow.cpp:26:59: error: invalid use of incomplete type ‘class std::thread’
                        t1_m = new std::thread(status_update_thread, this, halt_m);
                                                                                 ^
                      In file included from ../qt_proxy_model_test/mainwindow.cpp:1:0:
                      ../qt_proxy_model_test/mainwindow.hpp:9:7: error: forward declaration of ‘class std::thread’
                       class thread;
                             ^
                      ../qt_proxy_model_test/mainwindow.cpp:33:47: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
                        model_m.setHorizontalHeaderLabels(QStringList({"Tree Info"}));
                                                                     ^
                      ../qt_proxy_model_test/mainwindow.cpp:33:61: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
                        model_m.setHorizontalHeaderLabels(QStringList({"Tree Info"}));
                                                                                   ^
                      ../qt_proxy_model_test/mainwindow.cpp:33:61: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
                      ../qt_proxy_model_test/mainwindow.cpp:33:61: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
                      ../qt_proxy_model_test/mainwindow.cpp:33:61: error: call of overloaded ‘QStringList(<brace-enclosed initializer list>)’ is ambiguous
                      ../qt_proxy_model_test/mainwindow.cpp:33:61: note: candidates are:
                      In file included from /usr/include/qt4/QtGui/qcolor.h:47:0,
                                       from /usr/include/qt4/QtGui/qpalette.h:46,
                                       from /usr/include/qt4/QtGui/qwidget.h:50,
                                       from /usr/include/qt4/QtGui/qmainwindow.h:45,
                                       from /usr/include/qt4/QtGui/QMainWindow:1,
                                       from ../qt_proxy_model_test/mainwindow.hpp:4,
                                       from ../qt_proxy_model_test/mainwindow.cpp:1:
                      /usr/include/qt4/QtCore/qstringlist.h:71:12: note: QStringList::QStringList(const QStringList&)
                           inline QStringList(const QStringList &l) : QList<QString>(l) { }
                                  ^
                      /usr/include/qt4/QtCore/qstringlist.h:70:21: note: QStringList::QStringList(const QString&)
                           inline explicit QStringList(const QString &i) { append(i); }
                                           ^
                      ../qt_proxy_model_test/mainwindow.cpp: In destructor ‘virtual MainWindow::~MainWindow()’:
                      ../qt_proxy_model_test/mainwindow.cpp:40:6: error: invalid use of incomplete type ‘class std::thread’
                        t1_m->join();
                            ^
                      In file included from ../qt_proxy_model_test/mainwindow.cpp:1:0:
                      ../qt_proxy_model_test/mainwindow.hpp:9:7: error: forward declaration of ‘class std::thread’
                       class thread;
                             ^
                      make: *** [Makefile:233: mainwindow.o] Error 1
                      15:46:54: The process "/usr/bin/make" exited with code 2.
                      Error while building/deploying project qt_proxy_model_test (kit: Desktop)
                      When executing step "Make"
                      15:46:54: Elapsed time: 00:01.
                      

                      And this is the project file: qt_proxy_model_test.pro

                      #-------------------------------------------------
                      #
                      # Project created by QtCreator 2017-03-06T11:09:29
                      #
                      #-------------------------------------------------
                      
                      QT       += core gui
                      
                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                      
                      TARGET = qt_proxy_model_test
                      TEMPLATE = app
                      
                      # The following define makes your compiler emit warnings if you use
                      # any feature of Qt which as been marked as deprecated (the exact warnings
                      # depend on your compiler). Please consult the documentation of the
                      # deprecated API in order to know how to port your code away from it.
                      DEFINES += QT_DEPRECATED_WARNINGS
                      
                      # You can also make your code fail to compile if you use deprecated APIs.
                      # In order to do so, uncomment the following line.
                      # You can also select to disable deprecated APIs only up to a certain version of Qt.
                      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                      
                      
                      SOURCES += main.cpp\
                              mainwindow.cpp
                      
                      HEADERS  += mainwindow.hpp \
                          priv.hpp
                      
                      FORMS    += mainwindow.ui
                      
                      CONFIG += c++11
                      
                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Ok, I just double checked to be sure. CONFIG += c++11 seems to be Qt 5 only, so you can do something like:

                        greaterThan(QT_MAJOR_VERSION, 4) {    
                            CONFIG += c++11
                        } else {
                            QMAKE_CXXFLAGS += -std=c++11
                        }
                        

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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