My custom widget don't appear in window.



  • This is my custom widget class:
    @
    class GenoEyeWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit GenoEyeWidget(double page_w, double page_h,
    geno_eye::PosType genome_display_length,
    QWidget* parent = 0, Qt::WindowFlags f = 0);
    protected:
    virtual void paintEvent(QPaintEvent* e);
    //......
    };

    GenoEyeWidget::GenoEyeWidget(double page_w, double page_h,
    geno_eye::PosType genome_display_length,
    QWidget* parent, Qt::WindowFlags f) :
    QWidget(parent, f),
    //......
    {
    // ......
    resize(page_w, page_h);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    qDebug() << "size on construction:" << this->size() ;
    // ......
    

    }
    void GenoEyeWidget::paintEvent(QPaintEvent* e)
    {
    qDebug() << "on paint event";
    //......
    }
    @
    and this is the test code:
    @
    class t_genoeyewidget : public QMainWindow
    {
    friend int main(int argc, char** argv);
    Q_OBJECT

    public:
    explicit t_genoeyewidget(QWidget *parent = 0);
    ~t_genoeyewidget();

    private:
    Ui::t_genoeyewidget *ui;
    };

    t_genoeyewidget::t_genoeyewidget(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::t_genoeyewidget)
    {
    ui->setupUi(this);

    GenoEyeWidget* content = new GenoEyeWidget(400, 600, 1000);
    ui->scrollArea->setWidget(content);
    

    }

    t_genoeyewidget::~t_genoeyewidget()
    {
    delete ui;
    }

    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);

    t_genoeyewidget mw;
    mw.show();
    QWidget* content = mw.ui->scrollArea->widget();
    qDebug() << "main: content widget:" << content << "size"<<content->size();
    
    return app.exec();
    

    }
    @

    Everything else shows, but my custom widget isn't. Its onPaint() method is never called, and its size is (0,0) after mw.show():

    size on construction: QSize(400, 600)
    main: content widget: GenoEyeWidget(0x2542250) size QSize(0, 0)


  • Moderators

    Hi, welcome to devnet

    As documentation states the policy set by setSizePolicy is not used when widget is placed in a layout (as is in your case). Because you place your widget in a scroll area before it is shown it probably gets resized to 0x0.

    Try to change this
    @
    resize(page_w, page_h);
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    @
    to that
    @
    setFixedSize(page_w, page_h);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.