我需要能够访问来自exprr.cpp在mainwindow.h中声明的QList<QRadioButton *> colorList

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

主窗口

#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:已经在main.obj中定义了“类QList colorList”(?colorList @@ 3V?$ QList @ PAVQRadioButton @@@@ A)

下注

#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::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 ;

}

最佳答案

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

09-10 16:21