我不知道我的变量是否未被初始化或其他什么

时间:2015-11-03 12:30:50

标签: c++ qt

因此,在调试Qt中的程序后,我意识到调试器认为我没有初始化变量;但是,我从私有类中获取变量,使类成为指针,似乎没有任何反应。请让我知道我错过了什么,我在其他程序中遇到同样的问题,但我不知道是不是我或程序。

代码如下:

主:

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

和selectionarea.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 0 0 0 0 0

并显示窗口。

3 个答案:

答案 0 :(得分:3)

将我的评论作为答案

你的setter函数应该看起来像:

void SelectionArea::setStartingLocX(int x)
{
    StartingLocX = x;
}

因为您使用StartingLocX的值初始化类成员变量x(对于其他setter函数也是如此)。在您的函数版本中,您执行相反的操作,以便您的类成员变量保持未初始化。

答案 1 :(得分:1)

创建SelectionArea的实例时,不会初始化成员变量。您只能通过设置器功能设置它们。

您应该使用初始化列表在构造函数中初始化它们。

在这种情况下,这可能已经过时,但它会阻止您遇到奇怪的错误情况。

答案 2 :(得分:0)

这非常非惯用的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继承。