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
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
No, you have to rely on the default constructor if you want the widget created from within Designer generated code.
...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.
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
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.
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
Note, that you have to have proper Q_PROPERTY to do so!
That's not possible. And also not necessary. If you callMyButton(QPixmap pressed, QPixmap normal);
This is the point where i don't know how to call it.orQt Code:
To copy to clipboard, switch view to plain text modedoesn't matter.Qt Code:
MyButton mb;To copy to clipboard, switch view to plain text mode
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:
MyButton(); sizeHint()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 16:54.
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:
So no other function is called between and if you don't call sizeHint() in your own c-tor, there is no difference.Qt Code:
MyButton mb;To copy to clipboard, switch view to plain text mode
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:
But within this function I haven't the pixmap when I can't set it within the constructorQt Code:
//return normalButton.size(); }To copy to clipboard, switch view to plain text mode
I hope you could understand what i mean.
Regards
NoRulez
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.
Thanks for the hint, i'll give it a try
Regards
NoRulez
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)
Ok, i can't get it to work. Here is the sample code i have.
ownbutton.h:
ownbutton.cpp:Qt Code:
#ifndef OWNBUTTON_H #define OWNBUTTON_H #include <QtGui> #include <QPixmap> public: protected: private: QPixmap pixmap; }; #endif // OWNBUTTON_HTo copy to clipboard, switch view to plain text mode
In the designer, I used a simple QPushButton, and add a Pixmap Property "setImage".Qt Code:
#include "ownbutton.h" #include <QtGui> } qDebug() << "Pixmap size: " << pixmap.size(); return pixmap.size(); } painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap); } pixmap = _pixmap; } return pixmap; }To copy to clipboard, switch view to plain text mode
The value of the property is an image of the resource file.
Hope you can help me
Thanks in advance
Regards
NoRulez
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...
I've updated the code, but it also does nothing
ownbutton.cpp
Qt Code:
#include "ownbutton.h" #include <QtGui> } qDebug() << "Pixmap size: " << pixmap.size(); return pixmap.size(); } painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap); } void OwnButton::MyImage(const QString& image) { //pixmap = QPixmap(QString(":/") + image); } pixmap = _pixmap; } return pixmap; }To copy to clipboard, switch view to plain text modeQt Code:
#ifndef OWNBUTTON_H #define OWNBUTTON_H #include <QtGui> #include <QPixmap> public: void MyImage(const QString& image); protected: private: QPixmap pixmap; }; #endif // OWNBUTTON_HTo copy to clipboard, switch view to plain text mode
Regards
NoRulez
Last edited by NoRulez; 23rd June 2009 at 13:35.
You got me wrong! The setter for MyImage must accept a QString.
And how looks the uic generated code for your dynamic property?Qt Code:
OwnButton::setImage(const QString& image)To copy to clipboard, switch view to plain text mode
I have changed it to look like this:
Qt Code:
public: void setImage(const QString& image); protected:To copy to clipboard, switch view to plain text modeI got the following result without a button/image drawn.Qt Code:
painter.drawPixmap(0, 0, pixmap.size().width(), pixmap.size().height(), pixmap); } void OwnButton::setImage(const QString& image) { } return pixmap; }To copy to clipboard, switch view to plain text mode
Could you please give me a simple working example?
Thanks in advance
Regards
NoRulez
please make a minimal compilable example.
I've attached an example
Thanks in advance
Regards
NoRulez
Bookmarks