Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Call Constructor within Designer

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Call Constructor within Designer

    Hi @all,

    i have made a subclass of QPushButton.
    This I had successfully assigned using "Promote To".
    Is there a way to set some Variables for the Constructor anywhere?

    Best Regards
    NoRulez

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Call Constructor within Designer

    No, you have to rely on the default constructor if you want the widget created from within Designer generated code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    ...and if you want set your custom properties in the designer, you have to write a plugin for the designer, which is not so hard.

  4. #4
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    Could you provide me a sample or something similar to use custom properties within the designer? A pluginexample is in the documentation, but what about custom properties? If I'm right, than you mean Q_PROPERTY?

    Regards
    NoRulez

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    Quote Originally Posted by NoRulez View Post
    A pluginexample is in the documentation, but what about custom properties?
    It's also in that documentation! E.g. the "Container Extension" example. Look also at QDesignerPropertySheetExtension. But that is not necessary at all, since you also add dynamic properties directly in the designer: Use the plus near to the filter in the property editor.

  6. #6
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    OK, I've found it. It seems that this function is new in Qt 4.5.
    So, now I added 2 pixmaps as follows: pressedPixmap = :/images/pressedButton.png" and normalPixmap = ":/images/normalButton.png".

    Now i want to call my own constructor:
    MyButton(QPixmap pressed, QPixmap normal);

    This is the point where i don't know how to call it.

    Regards
    NoRulez

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    Quote Originally Posted by NoRulez View Post
    OK, I've found it. It seems that this function is new in Qt 4.5.
    So, now I added 2 pixmaps as follows: pressedPixmap = :/images/pressedButton.png" and normalPixmap = ":/images/normalButton.png".
    Note, that you have to have proper Q_PROPERTY to do so!

    MyButton(QPixmap pressed, QPixmap normal);

    This is the point where i don't know how to call it.
    That's not possible. And also not necessary. If you call
    Qt Code:
    1. MyButton mb(QPixmap pressed, QPixmap normal);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. MyButton mb;
    2. mb.setPressed(QPixmap pressed);
    3. mb.setNormal(QPixmap normal);
    To copy to clipboard, switch view to plain text mode 
    doesn't matter.

  8. #8
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    It does a matter, because after the constructor the sizeHint() function is called.
    The sizeHint() function should return the size of the image, otherwise the function returns 0x0 (width x height). At this point the sizeHint() returns 0x0, because the image is defined after the sizeHint() call.

    So if i call a function then the following are done:
    Qt Code:
    1. MyButton();
    2. sizeHint()
    3. setPressed(QPixmap pressed);
    4. setNormal(QPixmap normal);
    To copy to clipboard, switch view to plain text mode 

    I need to set the image before the sizeHint() function is called.
    So I thought the best way is the constructor.

    Regards
    NoRulez
    Last edited by NoRulez; 22nd June 2009 at 17:54.

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    Don't understand. For what do you mean the size hint is important? for the correct arrangement in a layout? it will be adjusted even if you set the images after the c-tor is called. And by the way, the designer is creating the instructions exactly that way:
    Qt Code:
    1. MyButton mb;
    2. mb.setPressed(QPixmap pressed);
    3. mb.setNormal(QPixmap normal);
    To copy to clipboard, switch view to plain text mode 
    So no other function is called between and if you don't call sizeHint() in your own c-tor, there is no difference.

  10. #10
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    The BaseClass (QPushButton) calls the sizeHint() function. If you use debug messages in the overloaded functions you will see that the sizeHint() is called after the constructor. After the sizeHint() the other functions are called.

    So when the size of my Pixmap is for example 250x130. then my button should also be the same size. To set the size of the button you could also write:
    Qt Code:
    1. QSize MyButton::sizeHint() const {
    2. return QSize(250,130);
    3. //return normalButton.size();
    4. }
    To copy to clipboard, switch view to plain text mode 
    But within this function I haven't the pixmap when I can't set it within the constructor

    I hope you could understand what i mean.

    Regards
    NoRulez

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Call Constructor within Designer

    There is no "after the constructor". Besides, the sizeHint() can change, just call QWidget::updateGeometry() from within the methods used to set up the images. sizeHint() will be called again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    Thanks for the hint, i'll give it a try

    Regards
    NoRulez

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    Have you tested the approach? Now I see that the designer produced code give a layout to the ctor, which will invoke the size hint, because it adds the widget to the layout. But nonetheless I think when showing the widget size hint is called again and then your images are already set and the function will return the right size.

    (But to call explicitly updateGeometry() is the saver way, also if you want to change the images at runtime)

  14. #14
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    Ok, i can't get it to work. Here is the sample code i have.
    ownbutton.h:
    Qt Code:
    1. #ifndef OWNBUTTON_H
    2. #define OWNBUTTON_H
    3.  
    4. #include <QtGui>
    5. #include <QPixmap>
    6.  
    7. class OwnButton : public QPushButton {
    8. Q_PROPERTY(QString MyImage READ getImage WRITE setImage)
    9.  
    10. public:
    11. OwnButton(QWidget *parent = 0);
    12. QSize sizeHint() const;
    13. void setImage(QPixmap _pixmap);
    14. QPixmap getImage();
    15.  
    16. protected:
    17. void paintEvent(QPaintEvent *event);
    18.  
    19. private:
    20. QPixmap pixmap;
    21. };
    22.  
    23. #endif // OWNBUTTON_H
    To copy to clipboard, switch view to plain text mode 
    ownbutton.cpp:
    Qt Code:
    1. #include "ownbutton.h"
    2. #include <QtGui>
    3.  
    4. OwnButton::OwnButton(QWidget *parent) : QPushButton(parent) {
    5. }
    6.  
    7. QSize OwnButton::sizeHint() const {
    8. qDebug() << "Pixmap size: " << pixmap.size();
    9. return pixmap.size();
    10. }
    11.  
    12. void OwnButton::paintEvent(QPaintEvent *event) {
    13. QPainter painter(this);
    14.  
    15. painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap);
    16. }
    17.  
    18. void OwnButton::setImage(QPixmap _pixmap) {
    19. pixmap = _pixmap;
    20. QWidget::updateGeometry();
    21. }
    22.  
    23. QPixmap OwnButton::getImage() {
    24. return pixmap;
    25. }
    To copy to clipboard, switch view to plain text mode 
    In the designer, I used a simple QPushButton, and add a Pixmap Property "setImage".
    The value of the property is an image of the resource file.

    Hope you can help me
    Thanks in advance

    Regards
    NoRulez

  15. #15
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    setImage is the setter not the property! So use MyImage (as you defined as a property), second, you need to provide a function which accept a QString, since this is the value the designer deals with when using embedded images. your QPixmap function is never called...

  16. #16
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    I've updated the code, but it also does nothing

    ownbutton.cpp
    Qt Code:
    1. #include "ownbutton.h"
    2. #include <QtGui>
    3.  
    4. OwnButton::OwnButton(QWidget *parent) : QPushButton(parent) {
    5. }
    6.  
    7. QSize OwnButton::sizeHint() const {
    8. qDebug() << "Pixmap size: " << pixmap.size();
    9. return pixmap.size();
    10. }
    11.  
    12. void OwnButton::paintEvent(QPaintEvent *event) {
    13. QPainter painter(this);
    14.  
    15. painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap);
    16. }
    17.  
    18. void OwnButton::MyImage(const QString& image) {
    19. pixmap = QPixmap(image);
    20. //pixmap = QPixmap(QString(":/") + image);
    21. QWidget::updateGeometry();
    22. }
    23.  
    24. void OwnButton::setImage(QPixmap _pixmap) {
    25. pixmap = _pixmap;
    26. QWidget::updateGeometry();
    27. }
    28.  
    29. QPixmap OwnButton::getImage() {
    30. return pixmap;
    31. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef OWNBUTTON_H
    2. #define OWNBUTTON_H
    3.  
    4. #include <QtGui>
    5. #include <QPixmap>
    6.  
    7. class OwnButton : public QPushButton {
    8. Q_PROPERTY(QString MyImage READ getImage WRITE setImage)
    9.  
    10. public:
    11. OwnButton(QWidget *parent = 0);
    12. QSize sizeHint() const;
    13. void MyImage(const QString& image);
    14. void setImage(QPixmap _pixmap);
    15. QPixmap getImage();
    16.  
    17. protected:
    18. void paintEvent(QPaintEvent *event);
    19.  
    20. private:
    21. QPixmap pixmap;
    22. };
    23.  
    24. #endif // OWNBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    Regards
    NoRulez
    Attached Images Attached Images
    Last edited by NoRulez; 23rd June 2009 at 14:35.

  17. #17
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    You got me wrong! The setter for MyImage must accept a QString.
    Qt Code:
    1. OwnButton::setImage(const QString& image)
    To copy to clipboard, switch view to plain text mode 
    And how looks the uic generated code for your dynamic property?

  18. #18
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    I have changed it to look like this:
    Qt Code:
    1. public:
    2. OwnButton(QWidget *parent = 0);
    3. QSize sizeHint() const;
    4. void setImage(const QString& image);
    5. QPixmap getImage();
    6.  
    7. protected:
    8. void paintEvent(QPaintEvent *event);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void OwnButton::paintEvent(QPaintEvent *event) {
    2. QPainter painter(this);
    3.  
    4. painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap);
    5. }
    6.  
    7. void OwnButton::setImage(const QString& image) {
    8. pixmap = QPixmap(image);
    9. QWidget::updateGeometry();
    10. }
    11.  
    12. QPixmap OwnButton::getImage() {
    13. return pixmap;
    14. }
    To copy to clipboard, switch view to plain text mode 
    I got the following result without a button/image drawn.

    Could you please give me a simple working example?

    Thanks in advance

    Regards
    NoRulez
    Attached Images Attached Images

  19. #19
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Call Constructor within Designer

    please make a minimal compilable example.

  20. #20
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Call Constructor within Designer

    I've attached an example

    Thanks in advance
    Regards
    NoRulez
    Attached Files Attached Files

Similar Threads

  1. designer not showing menu items ...
    By wagmare in forum Installation and Deployment
    Replies: 0
    Last Post: 24th February 2009, 11:26
  2. wwwidgets.how can it be brought into designer as plugin
    By sh123 in forum Installation and Deployment
    Replies: 1
    Last Post: 21st February 2009, 13:56
  3. Replies: 13
    Last Post: 15th December 2006, 12:52
  4. Designer crashes when selecting some widgets
    By gwendal in forum Qt Tools
    Replies: 4
    Last Post: 21st July 2006, 14:18
  5. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 21:46

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.