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. Simple Function Pointer Example
Forum Updated to NodeBB v4.3 + New Features

Simple Function Pointer Example

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 473 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.
  • K Offline
    K Offline
    kendavistoo
    wrote on 25 Dec 2018, 16:36 last edited by
    #1

    I have written a simple function pointer example which uses typedef. Hopefully it is useful for some of you all. If there are any errors or stupid mistakes, please comment on them.

    main.cpp:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.resize(300, 200);
        w.show();
    
        return a.exec();
    }
    

    SimpleFunctionPointer.pro:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-12-24T09:43:01
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = SimpleFunctionPointer
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has 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
    
    CONFIG += c++11
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp
    
    HEADERS += \
            mainwindow.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    mainwindow.cpp:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    #include <QRadioButton>
    #include <QButtonGroup>
    #include <QPainter>
    #include <QPaintEvent>
    #include <QWidget>
    
    static const int ORDER_NONE     = 0;
    static const int ORDER_ASCEND   = 1;
    static const int ORDER_DESCEND  = 2;
    
    //----------
    
    typedef bool (*ValidateFcn)(int&, int&);
    
    //---------- class MainWindow ----------
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        static bool    ascending(int&, int&);
        static bool    descending(int&, int&);
    
        void    drawArray();
    
        int array[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 };
        int arraySize = 9;
    
        QRadioButton*   ascendButt;
        QRadioButton*   descendButt;
    
        QButtonGroup*   buttGroup;
    
        ValidateFcn valFcn;
    
        int order = ORDER_NONE;
    
        bool doSort = false;
    
        bool doBubble(ValidateFcn);
    
    protected:
        void paintEvent(QPaintEvent*);
    
    public slots:
        void SLOT_AscendClick();
        void SLOT_DescendClick();
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp:

    #include "mainwindow.h"
    
    //---------- CONSTRUCTOR   MainWindow ----------
    
    MainWindow :: MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        ascendButt  = new QRadioButton("Ascend", this);
        descendButt = new QRadioButton("Descend", this);
    
        ascendButt->setGeometry (40, 50, 130, 24);
        descendButt->setGeometry(40, 90, 130, 24);
    
        buttGroup   = new QButtonGroup(this);
        buttGroup->addButton(ascendButt, 1);
        buttGroup->addButton(descendButt, 2);
        buttGroup->setExclusive(true);
    
        connect(ascendButt, &QAbstractButton::toggled, this, &MainWindow::SLOT_AscendClick);
        connect(descendButt, &QAbstractButton::toggled, this, &MainWindow::SLOT_DescendClick);
    }
    
    //---------- DESTRUCTOR   ~MainWindow ----------
    
    MainWindow :: ~MainWindow()
    {
    
    }
    
    //---------- paintEvent ----------
    
    void MainWindow :: paintEvent(QPaintEvent* event){
    
        QPainter* painter = new QPainter(this);
    
        //paint indicator
        int x = 10 + (int)(20 + (rand() % 10));
        int y = (this->height() - 30);
        QRect myRect(x,y,8,8);
        painter->fillRect(myRect, Qt::red);
    
        //----------
    
        if (doSort){
    
            doSort = false;
    
            switch(order){
    
            case ORDER_ASCEND:
                valFcn = ascending;
    
                if (doBubble(valFcn)){
                    doSort = true;
                }//if
                break;
    
            case ORDER_DESCEND:
                valFcn = descending;
    
                if (doBubble(valFcn)){
                    doSort = true;
                }//if
            }//switch
    
            if ( ! doSort){
                drawArray();
            }//if
        }//if doSort
    
        delete painter;
    
        update();
    }
    
    //---------- ascending ----------
    
    bool MainWindow :: ascending(int& a, int& b){
        return b < a;
    }
    
    //---------- descending ----------
    
    bool MainWindow :: descending(int& a, int& b){
        return b > a;
    }
    
    //---------- doBubble ----------
    
    bool MainWindow :: doBubble(ValidateFcn vFcn){
    
        bool didSwap = false;
    
        for (int i=0; i<(arraySize -1); i++){
    
            int a = array[i];
            int b = array[i+1];
            if ( (*vFcn)(a, b) ){
                int cpy = array[i];
                array[i] = array[i+1];
                array[i+1] = cpy;
                didSwap = true;
            }//if
        }//for i
    
        return didSwap;
    }
    
    //---------- drawArray ----------
    
    void MainWindow :: drawArray(){
    
        QString str;
        for (int i=0; i<9; i++){
            str.append(QString::number(array[i]));
            str.append(" ");
        }
        qDebug()<<str;
    }
    
    //---------- SLOT_AscendClick ----------
    
    void MainWindow :: SLOT_AscendClick(){
    
        if (ascendButt->isChecked()){
            order = ORDER_ASCEND;
            doSort = true;
            qDebug()<<"\nORDER_ASCEND";
            drawArray();
        }//if
    }
    
    //---------- SLOT_DescendClick ----------
    
    void MainWindow :: SLOT_DescendClick(){
    
        if (descendButt->isChecked()){
            order = ORDER_DESCEND;
            doSort = true;
            qDebug()<<"\nORDER_DESCEND";
            drawArray();
        }//if
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Dec 2018, 21:06 last edited by
      #2

      Hi,

      At least one thing: don't do the sorting un your paintEvent, it just consumes processing power at the wrong time. Do the sorting when you change its type and once done trigger the update to your UI.

      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

      1/2

      25 Dec 2018, 16:36

      • Login

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