Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My custom widget don't appear in window.

My custom widget don't appear in window.

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 602 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jiandingzhe
    wrote on last edited by
    #1

    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)

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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);
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved