在header中放置变量的位置
Where to put variable in header?
我需要能够访问在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;
相关文章:
- 非常量变量只读位置的赋值
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 在 elf 文件中查找全局变量的位置
- C++强制变量到一个固定的内存位置
- 当使用Lua作为嵌入式语言(比如c++)时,有什么简单/方便的方法可以找到变量在Lua中的定义位置吗
- 函数,返回变量c++占用的内存位置的大小
- 在 Visual Studio 2010(及更高版本)中定义包含或链接路径变量的位置
- 为什么动态分配的两个变量的内存位置不是连续的?
- ClangTool 如何获取变量声明中模板参数的位置?
- 如何跟踪通过外部代码修改类变量的所有位置
- 适用于会员变量的新位置
- C++每个变量的存储位置是否正确?
- 可视C 编译器将相同的存储位置提供给两个变量
- 双变量的精度仅为十点后的2个位置
- *的位置有什么区别:*变量与变量*
- 引用变量何时合适,为什么?你能解释一下实际的语法和位置吗?
- 局部类变量位置
- 运行时检查失败 #2 - 变量"位置"周围的堆栈已损坏
- 将cout引用到一个变量位置(文件或cmd)
- 单例的静态变量位置