QGC中MainWindow与QML

对QGC主窗口的学习

在ui\MainWindow.h中
构造方法被定义成私有的。

在QGCApplication.cc中__initForNormalAppBoot()中
创建窗口,不是new MainWindow,也是由于构造方法是私有的
采用
MainWindow *mainWindow=MainWindow::__create();方式创建

在_initForNormalAppBoot(void)执行时候:


QSettings settings;
_loadCurrentStyleSheet();

//上一个窗口关闭时候退出main application 
connect(this,&QGCApplication::lastWindowClosed,this,QGCApplication::quit);

在MainWindow的构造方法中,

_instance=this;
loadSettings();
emit initStatusChanged(tr('xxx'),Qt::AlignLeft|Qt::AlignBottom,QColor(62,93,141));

_ui.setupUi(this);

//确保在改变最小宽度之前,确保所有tool bar elements都fit

setMinimumWidth(1008);
configureWindowName();



//使用QVBoxLayout这个widget来装所有view
_centralLayout=new QVBoxLayout();
_centralLayout->setContentsMargins(0,0,0,0);

centralWidget()->setLayout(_centralLayout);

MainWindowHybrid.qml

Item{
    function showSetupView(){
        mainWindowInner.item.showSetupView()
    }
    function attemptWindowClose(){
        mainWindowInner.item.attemptWindowClose()
    }
    function showMessage(message){
        mainWindowInner.item.showMessage(message)
    }
}

在上述qml中的方法,在QGCApplication.cc中定义

void QGCApplication::showSetupView(void)
{
    QMetaObject::invokeMethod(_rootQmlObject(), "showSetupView");
}

void QGCApplication::qmlAttemptWindowClose(void)
{
    QMetaObject::invokeMethod(_rootQmlObject(), "attemptWindowClose");
}

void QGCApplication::showMessage(const QString& message)
{
    // Special case hack for ArduPilot prearm messages. These show up in the center of the map, so no need for popup.
    if (message.contains("PreArm:")) {
        return;
    }

    QObject* rootQmlObject = _rootQmlObject();

    if (rootQmlObject) {
        QVariant varReturn;
        QVariant varMessage = QVariant::fromValue(message);

        QMetaObject::invokeMethod(_rootQmlObject(), "showMessage", Q_RETURN_ARG(QVariant, varReturn), Q_ARG(QVariant, varMessage));
#ifndef __mobile__
    } else if (runningUnitTests()){
        // Unit test can run without a main window which will lead to no root qml object. Use QGCMessageBox instead
        QGCMessageBox::information("Unit Test", message);
#endif
    } else {
        qWarning() << "Internal error";
    }
}

在ui\MainWindow.h中

_mainQmlWidgetHolder->setContextPropertyObject("controller",this);
_mainQmlWidgetHolder-
>setContextPropertyObject("debugMessageModel",AppMEssage::getModel());
_mainQmlWidgetHolder->setSource(QUrl::fromUserInput("qrc:qml/MainWindowHybrid.qml"));

因此在qml中有个controller可以用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值