在header中放置变量的位置

Where to put variable in header?

本文关键字:变量 位置 header      更新时间:2023-10-16

我需要能够访问在mainwindow.h中声明的QList<QRadioButton *> colorList,从winger .cpp.

我将在这里显示我当前的尝试。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>
#include <Qlist>
#include <QRadioButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();
private slots:
    void runButtonClicked();
private:
    Ui::MainWindow *ui;
    QPushButton *runButton;
        QTextEdit * runText;
    };
 QList<QRadioButton *> colorList; // where should i put this??

    #endif // MAINWINDOW_H

错误:LNK2005: "class QList colorList" (?colorList@@ 3v ?$QList@PAVQRadioButton@@@@A)已在main.obj中定义

wager.cpp

#include "wager.h"
#include "mainwindow.h"
#include "deck.h"
Wager::Wager()
{
}
void build_bet_lists()
{
for(int i=0;i<5;i++)
    {
        qDebug()<<colorList[i]->isChecked;
}
}

colorList在

中定义

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
QRadioButton * street1BetBlack = new QRadioButton("Black");
QRadioButton * street2BetBlack = new QRadioButton("Black");
QRadioButton * street3BetBlack = new QRadioButton("Black");
QRadioButton * street4BetBlack = new QRadioButton("Black");
QRadioButton * street5BetBlack = new QRadioButton("Black");
QRadioButton * street1BetRed = new QRadioButton("Red");
QRadioButton * street2BetRed = new QRadioButton("Red");
QRadioButton * street3BetRed = new QRadioButton("Red");
QRadioButton * street4BetRed = new QRadioButton("Red");
QRadioButton * street5BetRed = new QRadioButton("Red");
QList<QRadioButton *> colorList;
colorList << street1BetBlack << street1BetRed << street2BetBlack << street2BetRed << street3BetBlack << street3BetRed << street4BetBlack << street4BetRed << street5BetBlack << street5BetRed ;
}

在header中声明,就像你做的那样:但是声明为extern:

extern QList<QRadioButton *> colorList; //declared

除了在头文件中"声明"它之外,还可以在CPP文件中"定义"它:

QList<QRadioButton *> colorList; //defined

在全局作用域中定义,而不是在MainWindow构造函数中定义:

QList<QRadioButton *> colorList;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

QRadioButton * street1BetBlack = new QRadioButton("Black");
QRadioButton * street2BetBlack = new QRadioButton("Black");
QRadioButton * street3BetBlack = new QRadioButton("Black");
QRadioButton * street4BetBlack = new QRadioButton("Black");
QRadioButton * street5BetBlack = new QRadioButton("Black");
QRadioButton * street1BetRed = new QRadioButton("Red");
QRadioButton * street2BetRed = new QRadioButton("Red");
QRadioButton * street3BetRed = new QRadioButton("Red");
QRadioButton * street4BetRed = new QRadioButton("Red");
QRadioButton * street5BetRed = new QRadioButton("Red");
colorList << street1BetBlack << street1BetRed << street2BetBlack << street2BetRed << street3BetBlack << street3BetRed << street4BetBlack << street4BetRed << street5BetBlack << street5BetRed ;

}

或者,您可以将其声明并定义为类的静态成员,如MainWindow。

必须使用extern关键字来访问c++中的全局变量。语法如下:

extern type varName;

那么在你声明变量的文件中试试这个:

extern QList<QRadioButton *> colorList;