Promoted CustomQScrollArea segfault
-
My actual use case is nontrivial, so I provide a small example that reproduces the segmentation fault:
I have class CustomScrollArea that inherits QScrollArea:
@
class CustomScrollArea : public QScrollArea
{
Q_OBJECT
public:
explicit WarxingScrollArea(QWidget* parent=0);
void segFaultThrowingMethod();
private:
QLabel* dynamicallyCreatedLabel;
};
@@
CustomScrollArea::CustomScrollArea(QWidget* parent) : QScrollArea(parent)
{
dynamicallyCreatedLabel = new QLabel;
setWidget(dynamicallyCreatedLabel);//this call will _not_ cause a seg fault segFaultThrowingMethod();
}
void CustomScrollArea::segFaultThrowingMethod()
{
dynamicallyCreatedLabel->setText("Some text");
}
@I have a MainWindow class with the following source:
@
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
@@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);//this call will cause a seg fault ui->customScrollArea->segFaultThrowingMethod();
}
MainWindow::~MainWindow()
{
delete ui;
}
@The .ui file for MainWindow contains a single QScrollArea promoted to a CustomQScrollArea. Its name is "customScrollArea".
When I start the application, the first invocation of CustomScrollArea::segFaultThrowingMethod() occurs in CustomScrollArea's constructor for Ui::MainWindow::customScrollArea and passes without incident. However, when MainWindow invokes ui->customScrollArea->segFaultThrowingMethod(), a segmentation fault occurs.
Note that the segmentation fault is not due to CustomScrollArea::dynamicallyCreatedLabel being null. The best clue I have is that this issue does not happen if MainWindow dynamically instantiates a CustomScrollArea rather than getting it from Ui::MainWindow. However, I would like to be able to use QScrollAreas promoted to CustomScrollAreas in Qt Designer.
Thanks for any help!
-
This has been answered at "stackoverflow":http://stackoverflow.com/questions/19987774/promoted-customqscrollarea-segmentation-fault. The problem is my promoting a QScrollArea. A promoted QScrollArea will have its constructor's setWidget() call overridden in ui->setupUi(). If I promote just a QWidget to a CustomScrollArea, I get around this. It feels like bad form, but it works.