How to access widgets in a subclass?
How to access widgets in a subclass?
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.
QLineEdit
resizeEvent

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
---------------------------
auto button = new QPushButton("...", this);
button = new QPushButton("...", this);
1 Answer
1
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.
auto button = new QPushButton("...", this);
button
You want button = new QPushButton("...", this); - or even better; move that to your constructors initialization list rather than using the ctor body.
button = new QPushButton("...", this);
Using the initialization list:
LineEditPath::LineEditPath(QWidget *parent) :
QLineEdit(parent),
button(new QPushButton("...", this))
{
You also never initialize your dialog member variable.
dialog
Could you demonstrate what you mean by moving it to my const unit list and the initializing of the dialog member please. I'm coming from python so stuff is a bit new to me. Thank you
– JokerMartini
Sep 14 '18 at 20:47
@JokerMartini Sure. Answer updated with initialization list example. As for what to do about
dialog, I don't know. I can just see you never initialize it - I have no idea what you intended to use it for or what you intended to initialize it with. All I can see is just an uninitialized variable that will blow up in your face if you use it.– Jesper Juhl
Sep 14 '18 at 20:56
dialog
The dialog itself will eventually be opened when the user clicks the Button. I haven't added that code yet :)
– JokerMartini
Sep 14 '18 at 21:03
@JokerMartini No. I was referring to your class having a member
QFileDialog *dialog; that I don't see you ever initializing to anything valid. So, if you ever try to dereference that pointer your program will exhibit Undefined Behaviour (probably explode). Either remove that variable if you don't need it or initialize it to something sane/usable.– Jesper Juhl
Sep 14 '18 at 21:07
QFileDialog *dialog;
@JokerMartini that is meaningless without a Minimal, Complete, and Verifiable example.
– Jesper Juhl
Sep 14 '18 at 21:17
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
change
auto button = new QPushButton("...", this);tobutton = new QPushButton("...", this);. the local variable is hiding the member of the class.– eyllanesc
Sep 14 '18 at 20:09