Я создал подкласс 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);
}
ошибка:
---------------------------
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
---------------------------