Using function pointers in QT

Source: Internet
Author: User

To imitate the command line, a class is defined so that a string corresponds to a function and both are entered into the list container.

class that defines the

    QString Commandstr;
    void (MainWindow::* commandfun) (void);
A qstring that points to a pointer to the MainWindow class member function.
But I didn't think it would be so complicated to use a function pointer in a class. In general, we use function pointer declarations and references that are straightforward and straightforward. But it's not the same in a class.
The final form of success is as follows:
Class Command_type{public:    command_type (QString str,void (MainWindow::* cfun) (void))    {        commandstr=str;        Commandfun=cfun;    }    QString Commandstr;    void (MainWindow::* commandfun) (void);}; Class Mainwindow:public qmainwindow{  //... Omit other             qlist<command_type> commandlist;//...} Mainwindow::mainwindow (Qwidget *parent):    Qmainwindow (Parent),    UI (new Ui::mainwindow) {
//...... Omit other commandlist<<command_type ("Refresh com", &mainwindow::enumserailinfo);} void MainWindow::p arsecommand (QString str) { qdebug () <<str; foreach (Command_type c,commandlist) { if (c.commandstr==str) { (this->* (C.commandfun)) (); Qdebug () << "Match";}} }

Statement:

void (MainWindow::* commandfun) (void);
In contrast to the normal function pointer declaration, here is the addition of MainWindow::, different classes are not universal.


Reference:
(this->* (C.commandfun)) ();
The this pointer is required here relative to the normal function pointer reference. If used in other classes or in the main function, you must use an object reference (the object has actual memory space).

Using function pointers in QT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.