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