PDA

View Full Version : Adding custom widget ( class ) into made in Qt Designer ui class



freely
13th August 2011, 18:31
Hello.
I have a custom widget class ( inherited from QWidget ) and an UI class made in Qt Designer and attached to mainwindow widget by ..->setupUi(…) method.
In the UI class I’ve put QWidget in Qt Designer ( empty region ) and I need to put inside it my custom widget class ( inherited from QWidget ).
I tried to make this by setting parent when construction custom widget class:

...
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
mc(new Ui::MainControl),intern(new Ui::Internal)
{
mc->setupUi(this);
intern->setupUi(this->mc->centralwidget);
//constructing image viewing:
image_preview=new ImageView();
image_preview->setParent(intern->widget_image);
...

But I get segmentation fault on Linux when I run the program. When I comment the last row:


//image_preview->setParent(intern->widget_image);

There is no seg. fault but image_preview is not added to intern UI class.
Does anybody know how to add the custom widget image_preview to the UI class member intern->widget_image , so the widget will be seen and active on the UI?
Thanks a lot for help.

NullPointer
14th August 2011, 02:35
Why don't you just promote the widget in the designer?
Right click in your "placeholder widget" and select "Promote to...", just fill the fields, add the class and click in Promote button...

Hth

freely
14th August 2011, 17:24
Hi,
Thanks for advice , I promoted it to my custom widget and I see in Object Inspector the promotion but when I preview in QtDesigner the custom widget does not seen and also I get segmentation fault when I run the program.
Why such? What also can be done?

NullPointer
14th August 2011, 18:14
Try passing the intern->widget_image to the constructor of the ImageView...
Then you will not need to call the setParent. Just show() the widget after constructor.

Hth.

freely
14th August 2011, 19:47
My custom widget is:

class ImageView : public QScrollArea {
Q_OBJECT
public:
ImageView(QWidget *parent=0);
...
I've made as you've advised:

//constructing image viewing:
image_preview=new ImageView(intern->widget_image);
But I still get segmentation fault when running the program.
What could be wrong?

Added after 20 minutes:

...I've tried also promote QScrolArea 'cause it is a closest parent of ImageView in place of QWidget , but still I get segmentation fault...:confused:
Why? Trying to fix this should work.

NullPointer
14th August 2011, 22:00
Are you able to use a


(new ImageView())->show();

??

I'm starting to suspect of other part of code...

Have you debugged the program?

freely
15th August 2011, 07:38
I've debugged and also added show() call:
I have MainWindow ( main program class ) that contains central widget that contains internal ui that contains QScrollArea that is promoted to custom widget ImageView.
Here it is the constructor of main window class:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
mc(new Ui::MainControl),intern(new Ui::Internal)
{
mc->setupUi(this);
intern->setupUi(this->mc->centralwidget);
//constructing image viewing:
image_preview=new ImageView(intern->scrollArea_for_imageView);
image_preview->show();

The segmentation fault happens when I run the compiled program when I call

this->centralWidget()->setVisible(true);
in the MainWindow class when I want to display the gui.
Does anyone know what is the problem,- why seg. fault?

freely
18th August 2011, 14:43
Hi. The problem is solved - there was a problem inside custom widget class - ImageView. The promotion in QtDesigner works well.
Thank you. The thread could be closed.