我不知道我的变量是否未初始化或其他原因
I don't know if my variables are not being initialized or something else
因此,在Qt中调试程序后,我意识到调试器认为我没有初始化变量;然而,我从私有类中获取了变量的输入和输出,使该类成为一个指针,似乎什么都没发生。请让我知道我错过了什么,我在其他程序中也有同样的问题,但我不知道是只有我还是这个程序。
代码如下:
main:
#include "selectionarea.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// QMainWindow *win = new QMainWindow();
QSize winsize(500,500);
SelectionArea area;
area.setStartingLocX(0);
area.setStartingLocY(0);
area.setLength(300);
area.setWidth(300);
area.resize(winsize);
area.show();
return app.exec();
}
selectionarea.cpp
#include "selectionarea.h"
#include <QPainter>
#include <QDebug>
#include <QLabel>
SelectionArea::SelectionArea()
{
}
void SelectionArea::paintEvent(QPaintEvent *)
{
QRect rectangle(getStartingLocx(), getStartingLocy(),
getWidth(), getLength());
/*QRegion Constructs a paint event object with the
* region that needs to be updated. The region is
* specified by paintRegion.*/
QPainter painter(this);
painter.setPen(QPen(Qt::SolidPattern,
2.0,
Qt::SolidLine,
Qt::FlatCap,
Qt::MiterJoin));
painter.drawRect(rectangle);
}
void SelectionArea::setStartingLocX(int x)
{
x=StartingLocX;
qDebug() <<x<<" "<<StartingLocX;
}
int SelectionArea::getStartingLocx()
{
return StartingLocX;
}
void SelectionArea::setStartingLocY(int y)
{
y=StartingLocY;
qDebug() <<y<<" "<<StartingLocY;
}
int SelectionArea::getStartingLocy()
{
return StartingLocY;
}
void SelectionArea::setWidth(int w)
{
w=Width;
qDebug() <<w<<" "<<Width;
}
int SelectionArea::getWidth()
{
return Width;
}
void SelectionArea::setLength(int l)
{
l=Length;
}
int SelectionArea::getLength()
{
return Length;
}
和选择rea.h
#ifndef SELECTIONAREA_H
#define SELECTIONAREA_H
#include <QPixmap>
#include <QLabel>
#include <QRect>
class SelectionArea : public QLabel
{
int StartingLocX;
int StartingLocY;
int Length;
int Width;
public:
SelectionArea();
~SelectionArea()
{
}
void setStartingLocX(int);
void setStartingLocY(int);
void setLength(int);
void setWidth(int);
int getStartingLocx();
int getStartingLocy();
int getWidth();
int getLength();
virtual void paintEvent(QPaintEvent *);
};
#endif // SELECTIONAREA_H
原谅我的含糊。
更新:应用程序输出为0 00 00 0
并且显示该窗口。
让我的评论成为答案
您的setter函数实际上应该看起来像:
void SelectionArea::setStartingLocX(int x)
{
StartingLocX = x;
}
因为您使用值x
初始化类成员变量StartingLocX
(对于其他setter函数也是如此)。在您的函数版本中,您可以执行相反的操作,以便类成员变量保持未初始化状态。
创建SelectionArea
实例时不初始化成员变量。您只能在之后通过setter函数设置它们。
您应该在构造函数中使用初始值设定项列表来初始化它们。
在这种情况下,这可能是过时的,但它将防止您遇到奇怪的错误情况。
这是非常不地道的C++:
SelectionArea area;
area.setStartingLocX(0);
area.setStartingLocY(0);
area.setLength(300);
area.setWidth(300);
您应该重构代码以读取:
SelectionArea area{0, 0, 300, 300};
甚至
SelectionArea area{300, 300};
此外,你真的应该让它成为一个真正的QObject
,而不是一个半成品。不要用自己相似的名字来掩盖QWidget
自己的成员(比如witdth()
等)。Getter不必有get
前缀,它只会增加视觉混乱,没有帮助。
我在下面添加的属性可能有些过头了,只需添加你合理可能使用的属性即可。简单地将整个选择保留为QRect
仍然是一个好主意——这就是它的真实情况
class SelectionArea : public QLabel
{
Q_OBJECT
Q_PROPERTY(QRect selection READ selection WRITE setSelection USER true)
Q_PROPERTY(QPoint selTopLeft READ selTopLeft WRITE setSelTopLeft STORED false)
Q_PROPERTY(int selX READ selX WRITE setSelX STORED false)
Q_PROPERTY(int selY READ sely WRITE setSelY STORED false)
Q_PROPERTY(QSize selSize READ selSize WRITE setSelSize STORED false)
Q_PROPERTY(int selWidth READ selWidth WRITE setSelWidth STORED false)
Q_PROPERTY(int selHeight READ selHeight WRITE setSelHeight STORED false)
QRect m_selection;
public:
SelectionArea(QWidget * parent = 0) : QLabel(parent) {}
SelectionArea(const QSize & size, QWidget * parent = 0) :
QLabel(parent), m_selection(QPoint(), size) {}
SelectionArea(const QPoint & startingLoc, const QSize & size,
QWidget * parent = 0) :
QLabel(parent), m_selection(startingLoc, size) {}
SelectionArea(int width, int height, QWidget * parent = 0) :
QLabel(parent), m_selection(0, 0, width, height) {}
SelectionArea(int x, int y, int width, int height, QWidget * parent = 0) :
QLabel(parent), m_selection(x, y, width, height) {}
void setSelection(const QRect & r) {
if (m_selection == r) return;
m_selection = r;
update();
}
void setSelTopLeft(const QPoint & p) {
if (m_selection.topLeft() == p) return;
m_selection.moveTo(p);
update();
}
void setSelX(int x) {
if (m_selection.x() == x) return;
m_selection.moveTo(x, m_selection.y());
update();
}
void setSelY(int y) {
if (m_selection.y() == y) return;
m_selection.moveTo(m_selection.x(), y);
update();
}
void setSelSize(const QSize & s) {
if (m_selection.size() == s) return;
m_selection.setSize(s);
update();
}
// etc.
QRect selection() const { return m_selection; }
QPoint selTopLeft() const { return m_selection.topLeft(); }
int selX() const { return m_selection.x(); }
int selY() const { return m_selection.y(); }
QSize selSize() const { return m_selection.size(); }
// etc.
protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
};
同样值得怀疑的是,您是否真的想从QLabel
派生。您似乎覆盖了绘画,并且实际上并不直接关心text
属性。您最好直接从QFrame
继承。
相关文章:
- 如何在线程中初始化对象,然后在其他地方使用它?
- lambda 作为接受其他参数的参数的初始化顺序
- 使用其他成员初始化结构的成员?
- 如何在需要提及需要循环声明的其他类的类中初始化变量?
- 使用全局变量初始化不同编译单元中的其他全局变量
- C++ 编译时检查微控制器引脚是否已从其他源文件初始化
- 与其他静态const成员初始化静态常量成员
- 如何在其他一个C 之前调用函数而不将其初始化
- 使用传入数组初始化其他数组
- 使用构造函数的可变参数中的其他模板化类执行模板化类的初始化
- 是否可以使用初始值设定项列表中的其他类成员初始化类成员
- 从另一个 std::optional 和其他东西初始化 std::optional 的惯用方法
- 是其他类型的不同类型的初始化衍生物,或者它们是完全分开的
- 如何初始化类的数据成员,以便这些数据成员是其他类的对象(在C++中)
- 成员变量是否可用于初始化列表中的其他成员
- 为什么类的 reinterpret_cast<const char*> 及其预初始化成员 int 相同,而任何其他类型的变量都不是?
- C++11 指向同一基类的其他类的指针的非静态数据成员统一初始化失败
- MPI如何使在根进程中初始化的变量在其他进程中可见
- C++11;非静态数据成员初始化是否可以访问其他数据成员
- 使用整数常量0初始化实例,但不使用其他常数值或整数变量