我不知道我的变量是否未初始化或其他原因

I don't know if my variables are not being initialized or something else

本文关键字:其他 初始化 我的 变量 是否 我不知道      更新时间:2023-10-16

因此,在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继承。