Important: Please read the Qt Code of Conduct -

[Solved]Adding custom widget ( class ) into made in Qt Designer ui class

  • 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) :
    mc(new Ui::MainControl),intern(new Ui::Internal)
    //constructing image viewing:
    image_preview=new ImageView();
    But I get segmentation fault on Linux when I run the program. When I comment the last row:
    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.

  • I tried also to promote it to my custom widget and I see in Qt Designer's 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?

  • My custom widget is:
    @class ImageView : public QScrollArea {
    ImageView(QWidget *parent=0);
    I've made in addition to promoting to custom widget at Qt Designer :
    @//constructing image viewing:
    image_preview=new ImageView(intern->widget_image);@
    But I still get segmentation fault when running the program.
    What could be wrong?

    • you need a layout on the intern->widget_image
    • don't call setParent, but call the constructor with the parent

  • you need a layout on the intern->widget_image
    Ok I've added vertical layout but still I get segmentation fault yet. I try to fix.
    If my custom widget inherited from QScrollArea

    1. can I promote in Qt Designer QWidget to ImageView?
    2. If I add QScrollArea from Widget Box in Qt Designer to the Ui I see in Object Inspector actually 2 widgets added QScrollArea and scrollAreaWidgetContents, what widget should I promote to my custom widget ImageView so as to get it visible and active at the Ui?
      don’t call setParent, but call the constructor with the parent
      I passed the ui's widget as the parent parameter to the custom widget constructor.

  • Sorry, I never used promoting widgets in designer. I usually just put a QWidget as a dummy container and add a layout and my custom widget later in C++.

  • Sorry, I never used promoting widgets in designer. I usually just put a QWidget as a dummy container and add a layout and my custom widget later in C++.
    Ok. I added layout and the custom widget to it inside ui_internal.h ( uic generated ) as:
    @ImageView *image_view_widget;@
    @ image_view_widget = new ImageView();
    But I can't access the image_view_widget as another public members of
    @class Ui_Internal
    yet in the code by intern->image_view_widget . Volker , Can you show some code example ? If no , ok I continue to resolve this issue it will be resolved of course with some time. Perhaps anybody else will help with this case.
    There is additional way to add custom widget to ui class - to make Qt Designer plugin ( custom widget plugin ) , but now actually I need to add custom widget without compiling and adding it as a Designer plugin. Perhaps it will be made later - then we will have new widget inside Qt Designer Widget Box - special image processing widget , if it will be serious need in that I will make.

  • I will put a small example. Just for the moment:

    Do not modify the ui_internal.h file - it is regenerated as soon as you clean and rebuild your project or you modify the .ui file it is generated from.

    Also, you should try to instantiate the widgets on small test cases to see wether they work at all and then move forward to the more complex setup.

    Also, replace your custom widgets with known to work widgets like a QTextEdit.

  • For Qt designer, plugin method will generate the identical *.ui file as promotion method. So making a plugin will not help you to sovle the problem.

    Promotion will be the best method for you. If you still come with problem, you need to debug your code.And I think that the bug may be hidden in the place which you did not give here.

  • What widget to put to Qt Designer form appropriated for promotion to ImageView inherited from QScrollArea?
    I've made promotion and I've got segmentation fault in place when this->centralWidget()->setVisible(true); called ( centralWidget contains intern ui that contains promoted to ImageView widget ). ( it is result of the debugging ).
    I will provide later more information.Thanks for help, but the problem is not yet solved

  • Hi. The problem is solved - there was a problem inside custom widget class - ImageView , not in the code for promotion, ui building. The promotion in QtDesigner works well.
    Thank you. The thread could be closed.

  • Glad to hear the problem is solved. You can "close" the thread by editing your very first post and add "[Solved]" to the beginning of the subject.

Log in to reply