【问题标题】:How can I use common variable in multiple cpp file in Qt..?如何在 Qt.. 的多个 cpp 文件中使用公共变量?
【发布时间】:2015-04-24 19:15:17
【问题描述】:
 #include "main.h"    
 #include "ui_main.h"     

 Main::Main(QWidget *parent):    
 QMainWindow(parent),    
 ui(new Ui::Main)    
 {    
 ui->setupUi(this);    
 }

 Main::~Main()    
 {    
     delete ui;    
 }

我有这个头文件主要用于 Qt 我应该在哪个部分创建全局变量

【问题讨论】:

  • 你不应该创建全局变量!
  • 你想做什么?如果您阅读了一些关于 qt 和 c++ 的教程或书籍,并查看安装文件夹中的示例,也许它会帮助您入门。
  • 谢谢 Thalia... 实际上我想在一些页面之间使用一些变量的值,所以这就是为什么我想让它成为全局的..
  • 我会说全局变量是可以的,只要你明智和谨慎地使用它们。话虽如此,对于特定问题可能有更优雅的解决方案。

标签: c++ qt variables


【解决方案1】:
    You can make use of extern variables. declare and define those variables
 in any .h or .cpp file and you can use them in other files using extern 
keyword and that variable name.

    e.g. in my main.cpp I have Qstring A = "xyz";
    suppose I have mainwindow.h and mainwindow.cpp
    in mainwindow.h
    extern Qstring A;
     you will get the value of A in mainwindow.

【讨论】:

    【解决方案2】:

    如果你真的想这样做,那么你需要实现一个Singleton 类。

    然后你可以像这样使用它:

    class Common final
    {
    public:
        Common& instance() // In the cpp.
        {
            static Common common;
            return common;
        }
    
        static int value() // In the cpp.
        {
            return 11;
        }
    
        static std::string text() // In the cpp.
        {
            return "desired text";
        }
    
    private:
        Common() // In the cpp.
        {
        }
    };
    

    或者只是在单独的头文件中添加一些变量,但使用OO 我认为在类中(或至少在命名空间中)更好。

    【讨论】:

      【解决方案3】:

      要在另一个文件中使用文件的变量...

      1.在头文件中公开变量...

      2.将该头文件包含在另一个 .cpp 文件中...

      3.使用该文件的变量创建文件的对象并使用成员运算符...

      //Main header file...
      
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include "a.h"
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
         Q_OBJECT
      
       public:
       explicit MainWindow(QWidget *parent = 0);
       int i=10;
       ~MainWindow();
      
        private slots:
        void on_pushButton_clicked();
      
        private:
        Ui::MainWindow *ui;
        };
      
      
         #endif // MAINWINDOW_H
      
      
      
      //in cpp file og
      #include "ui_mainwindow.h"
      
      
       MainWindow::MainWindow(QWidget *parent) :
       QMainWindow(parent),
       ui(new Ui::MainWindow)
       {
       ui->setupUi(this);
        }
      
        MainWindow::~MainWindow()
        {
        delete ui;
       }
      
       void MainWindow::on_pushButton_clicked()
       {
        a obj;
         obj.setModal(true);
         obj.exec();
       }
      

      //要在其中使用变量的cpp文件... //这里将头文件的一个对象设为使用成员操作符...

      #include "a.h"
      #include "ui_a.h"
      #include "mainwindow.h"
      
      a::a(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::a)
      {
        ui->setupUi(this);
      }
      
      a::~a()
      {
        delete ui;
      }
      
      void a::on_pushButton_clicked()
      {
        MainWindow obj;
        qDebug()<<obj.i;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多