Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Adding custom widget ( class ) into made in Qt Designer ui class
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 17.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swhweng
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      swhweng
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        swhweng
        wrote on last edited by
        #3

        My custom widget is:
        @class ImageView : public QScrollArea {
        Q_OBJECT
        public:
        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?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4
          • you need a layout on the intern->widget_image
          • don't call setParent, but call the constructor with the parent

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • S Offline
            S Offline
            swhweng
            wrote on last edited by
            #5

            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.
            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              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++.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • S Offline
                S Offline
                swhweng
                wrote on last edited by
                #7

                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;@
                and
                @ image_view_widget = new ImageView();
                verticalLayout_2->addWidget(image_view_widget);@
                But I can't access the image_view_widget as another public members of
                @class Ui_Internal
                {
                public:@
                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      swhweng
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        swhweng
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          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.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved