Results 1 to 20 of 23

Thread: Call Constructor within Designer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  3. #3
    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

  4. #4
    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)

  5. #5
    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

  6. #6
    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...

  7. #7
    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 13:35.

  8. #8
    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?

  9. #9
    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

  10. #10
    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.

  11. #11
    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

  12. #12
    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

    • use Q_OBJECT in your class OwnButton
    • use the right property name in the designer! (MyImage not setImate)

    and then, if you set QPixmap as a value in the custom property folowing is created:
    Qt Code:
    1. pushButton->setProperty("MyImage", QVariant(QPixmap(QString::fromUtf8(":/button_1.png"))));
    To copy to clipboard, switch view to plain text mode 
    So you have to provide QPixmap-setter like in your first code post, but also inside the Q_PROPERTY! then it works.

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

    Default Re: Call Constructor within Designer

    You forgot the Q_OBJECT macro.
    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.


Similar Threads

  1. designer not showing menu items ...
    By wagmare in forum Installation and Deployment
    Replies: 0
    Last Post: 24th February 2009, 10: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, 12:56
  3. Replies: 13
    Last Post: 15th December 2006, 11:52
  4. Designer crashes when selecting some widgets
    By gwendal in forum Qt Tools
    Replies: 4
    Last Post: 21st July 2006, 13: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, 20: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
  •  
Qt is a trademark of The Qt Company.