【问题标题】:How to break up Qt code into separate parts?如何将 Qt 代码分解为单独的部分?
【发布时间】:2013-11-04 03:07:24
【问题描述】:

所以我目前正在编写一个 Qt 应用程序,但对它相当陌生,并且不确定应该如何设计某些东西。随着我添加越来越多的代码,我的MainWindow.cpp 变得越来越大而且不守规矩。我很好奇将我的代码分成更小的文件的正确方法是什么。我希望移动到单独文件的每个组件都在对 UI 进行更改。我目前正在做的实际上只是创建一个新的 .cpp 文件,然后包括我的 MainWindow 和 MainWindow ui。这是我放置在其自己的文件中的函数示例。

#include <QDebug>
#include <QString>
#include <QPalette>
#include "master_main_window.h"
#include "ui_master_main_window.h"
#include "cmd_net.h"
#include "cmd.h"


/*
 * Send a command/data packet to the host
 */
void MasterMainWindow::sendCommand() {

    // Disable some GUI components
    ui->con_btn_cmd_disc->setEnabled(false);
    ui->con_btn_cmd_rec->setEnabled(false);
    ui->cmd_edit->setEnabled(false);
    ui->cmd_btn_send->setEnabled(false);

    // Send the packet through the open socket
    sendCmdPacket(ui->cmd_edit->text().toLocal8Bit().data(), cmdSocket);

    // Enable the socket notifier
    cmdSockNotifier->setEnabled(true);

    qDebug() << "Command sent:"
             << (ui->cmd_edit->text().toLocal8Bit().data())
             << "\nSent Packet";
}

如您所见,我简单地包含了“master_main_window.h”和“ui_master_main_window.h”,这使我可以访问 MainWindow 类中可用的所有不同函数/变量/ui。 我很好奇我是否以正确的方式执行此操作,或者是否有更好的方法来实现将函数拆分为单独文件的目标。

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    如果我得到您的要求正确,因为您在这里使用指针,您可以简单地在不同的文件中编写其他类并将ui 中的变量传递给它们。 例如,假设您是 ui,其中包含以下变量:

    QWidget * leftWidget;
    QWidget * centerWidget;
    QWidget * rightWidget;
    

    您可以编写继承QWidget 的类并将这些变量赋予它们,如下所示:

    class leftWidgetClass : public QWidget
    {
      //your code here
    }
    

    等等…… 然后在MainWindow 的构造函数中,您可以这样做:

    leftWidget = new leftWidgetClass();
    rightWidget = new rightWidgetClass();
    centerWidget = new centerWidgetClass();
    

    【讨论】:

      【解决方案2】:

      正确的方法是不要在mainwindow 类之外使用ui 命名空间和mainwindow 方法。您应该继承QWidget 或任何其他您想要扩展的功能的类,创建您需要的所有功能(如doImportantStuff())并在mainwindow.h 中包含这个新的类头(我们称之为myWidget)。然后您可以在主窗口中创建那些myWidget,将它们添加到ui(通过 disigner 以太,添加 QWidget,单击提升到,选择您的类,或通过手动添加到布局)并使用它们的所有信号和功能, 你创建的, 比如:

      connect(ui->doWorkButtot(),SIGNAL(clicked()), myWidget, SLOT(doImportantStuff()));`
      

      再次,您可以通过signals and slots mechanism 更改为ui;在myWidget 中,当你感觉到时,你必须以某种方式更改ui,emit 一个信号并在mainwindowconnect 中捕获它。示例:

      myWidget.h:
      
      ...
      signals:
      void needUiChange();
      ....
      public slots:
      myWidget::doImportantStuff(){
      
      ....
      
      emit needUiChange();
      //  you can emit signals with params also:
      //  emit setToolTipText(QString("from signal));
      ...
      

      并在主窗口中使用连接捕获信号:

      connect(myWidget, SIGNAL(needUiChange(),this,SLOT(updateUI());
      // actually you can connect directly to let's say `pushButton`'s slot `hide()`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-24
        • 2011-05-02
        • 2021-07-16
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多