一步步学Qt,第三天-Qt动态调用机制

一步步学Qt,第三天-Qt动态调用机制


今天写了一个程序,却发现了一个很有意思的东西,不知道是我理解不对,还是Qt确实如此


我要说的是Qt的动态调用机制,我们知道,在C++中,继承关系衍生的对象,他们可以来自fatherclass或者son class,在程序中会自动的找打对应的调用关系,比如在用了son的一个对象去调用一个函数,这个函数在base中已经有了,这样的话在程序中就会自动的去调用son的对应函数.


但是在qt中貌似不是的!!!

看例子:

项目结构:

在mainwindow.h中:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "showalldialog.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow,showAllDialog
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void showBody();
private:
    Ui::MainWindow *ui;
    showAllDialog *displayall;
private slots:
    void showMore();
    void showAll();
    void myclose();
};

#endif // MAINWINDOW_H

code中看出,Mainwindow继承了showAllDialog

再看看showAllDialog

#ifndef SHOWALLDIALOG_H
#define SHOWALLDIALOG_H

#include <QDialog>

namespace Ui {
    class showAllDialog;
}

class showAllDialog : public QDialog
{
    Q_OBJECT

public:
    explicit showAllDialog(QWidget *parent = 0);
    ~showAllDialog();
    void setInfo(const QString &info);
    QString getInfo();
    void viewInfo();
private:
    Ui::showAllDialog *ui;
    QString info;
};

#endif // SHOWALLDIALOG_H

code中显示showAllDialog继承了QDialog

也就是说在MainWindow(同时继承了QMainWindowshowAllDialog)有了两个show()函数在MainWindow

看看main()函数

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

看看编译结果:

mingw32-make[1]: Leaving directory `E:/WorkPlace/Qt/ExtensionWidget-build-desktop'
../ExtensionWidget/main.cpp: In function 'int qMain(int, char**)':
../ExtensionWidget/main.cpp:8: error: request for member 'show' is ambiguous
d:/StudyAppsInstall/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtGui/qwidget.h:487: error: candidates are: void QWidget::show()
d:/StudyAppsInstall/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtGui/qwidget.h:487: error:                 void QWidget::show()
mingw32-make[1]: *** [debug/main.o] Error 1
mingw32-make: *** [debug] Error 2
The process "D:\StudyAppsInstall\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project ExtensionWidget (target: Desktop)
When executing build step 'Make'

看到这个就知道问题出在show()之上.

这也就是说,在qt中没有去自动的处理这个机制

程序修改办法:

取消一个show()函数,也就是说需要在MainWindow之用一个show()出现,看代码

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "showalldialog.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void showBody();
private:
    Ui::MainWindow *ui;
    showAllDialog *displayall;
private slots:
    void showMore();
    void showAll();
    void myclose();
};

#endif // MAINWINDOW_H

看到MainWindow只是继承了QMainWindow也就是说这样在MainWindow这有一个show()存在.main()不变,执行成功


E:\WorkPlace\Qt\ExtensionWidget-build-desktop\debug\ExtensionWidget.exe exited with code 0



posted @ 2011-08-26 20:15  Podevor  阅读(526)  评论(0编辑  收藏  举报