0

我需要能够QList<QRadioButton *> colorList从 wager.cpp 访问在 mainwindow.h 中声明的内容。

我将在这里展示我目前的尝试。

主窗口.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 中定义

赌注.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 定义在

主窗口.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 ;

}
4

2 回答 2

2

像您一样在标题中声明它:但将其声明为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 等类的静态成员。

于 2012-12-29T05:28:03.730 回答
0

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

extern type varName;

所以在你声明变量的文件中试试这个:

extern QList<QRadioButton *> colorList;
于 2012-12-29T05:27:53.933 回答