Results 1 to 2 of 2

Thread: How to inherit Qt designer create a ui and class?

  1. #1
    Join Date
    Apr 2016
    Posts
    37
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to inherit Qt designer create a ui and class?

    hi , all.

    i use Qtcreator creator Qt designer ui and class .

    i try use the ui pointer in my child class.

    parent class code , in widget.h :
    Qt Code:
    1. namespace Ui {
    2. class Widget;
    3. }
    4. class Widget : public QWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit Widget(QWidget *parent = 0);
    9. ~Widget();
    10. Ui::Widget *ui; //ui is public
    11. private slots:
    12. void on_pushButton_clicked();
    13. };
    To copy to clipboard, switch view to plain text mode 

    child class code , in widget_child.h :
    Qt Code:
    1. class Widget_Child : public Widget
    2. {
    3. public:
    4. WidgetB_Child(QWidget * parent = 0);
    5. };
    To copy to clipboard, switch view to plain text mode 

    child class code , in widget_child.cpp :
    Qt Code:
    1. Widget_Child::Widget_Child(QWidget * parent) :
    2. Widget(parent)
    3. {
    4. ui->pushButton->setText("Widget_Child");
    5. }
    To copy to clipboard, switch view to plain text mode 

    a compiler error:
    E:\QtCode\Test\widget_child.cpp:6: error: request for member 'pushButton' in '((Widget_Child*)this)->Widget_Child::<anonymous>.Widget::ui', which is of pointer type 'Ui::Widget*' (maybe you meant to use '->' ?)
    ui.pushButton->setText("Widget_Child");
    ^

    i use QtCreator 4.0 Qt5.4.2 , Win7 64bit


    Added after 18 minutes:


    i add #include<ui_widget.h> is no problem.

    but , I don't know public:Ui::Widget *ui; whether the effect was encapsulation ?
    Last edited by tiaoweiliao; 22nd February 2017 at 08:05.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to inherit Qt designer create a ui and class?

    You should read the compiler error.

    The code you posted is the solution to the problem reported by it.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 4th February 2016, 22:32
  2. Replies: 5
    Last Post: 12th May 2014, 11:07
  3. Replies: 1
    Last Post: 22nd November 2013, 17:18
  4. Replies: 7
    Last Post: 18th August 2011, 14:43
  5. Using UI class from Designer?
    By miivers in forum Newbie
    Replies: 3
    Last Post: 21st September 2010, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.