Reputation: 6147
I have created a subclass of QLineEdit
and I am adding a button to the right side of the widget, so I can make an all-in-one path browsing control. However, I need to access the button from within the resizeEvent
, so I can properly place the button on the right side of the line edit. I get errors and I am assuming it is due to the way I have created the button.
lineeditpath.h
#ifndef LINEEDITPATH_H
#define LINEEDITPATH_H
#include <QLineEdit>
#include <QFileDialog>
#include <QPushButton>
class LineEditPath : public QLineEdit
{
Q_OBJECT
public:
explicit LineEditPath(QWidget *parent = 0);
signals:
public slots:
protected:
void resizeEvent(QResizeEvent *event) override;
private:
QFileDialog *dialog;
QPushButton *button;
};
#endif // LINEEDITPATH_H
lineedithpath.cpp
#include "lineeditpath.h"
#include <QLineEdit>
#include <QPushButton>
LineEditPath::LineEditPath(QWidget *parent) : QLineEdit(parent) {
setAcceptDrops(true);
auto button = new QPushButton("...", this);
button->setFixedWidth(30);
button->setCursor(Qt::CursorShape::PointingHandCursor);
button->setFocusPolicy(Qt::FocusPolicy::NoFocus);
setTextMargins(0,0,30,0); }
void LineEditPath::resizeEvent(QResizeEvent *event) {
QLineEdit::resizeEvent(event);
// Resize the button: ERROR
button->move(width()-button->sizeHint().width(), 0);
}
error:
---------------------------
Signal Received
---------------------------
<p>The inferior stopped because it received a signal from the operating system.<p><table><tr><td>Signal name : </td><td>SIGSEGV</td></tr><tr><td>Signal meaning : </td><td>Segmentation fault</td></tr></table>
---------------------------
OK
---------------------------
Upvotes: 1
Views: 91
Reputation: 31468
auto button = new QPushButton("...", this);
in your constructor does not initialize the button
member variable of your class. It creates a new local variable with the same name that shadows the member variable and goes out of scope at the end of the constructor body. Your member variable is never initialized.
You want button = new QPushButton("...", this);
- or even better; move that to your constructors initialization list rather than using the ctor body.
Using the initialization list:
LineEditPath::LineEditPath(QWidget *parent) :
QLineEdit(parent),
button(new QPushButton("...", this))
{
You also never initialize your dialog
member variable.
Upvotes: 2