Important: Please read the Qt Code of Conduct -

What is the easiest way to desing an "edit mode" via my GUI in QtDesigner?

  • Hey guys,

    How would you implement an "edit mode" in QT Designer? I have created my GUI. To view the data, I want the GUI to look like this:


    To edit the data and thus the "edit mode" I would like to build the GUI like this:


    Currently I have built the GUI over a QStackWidget. As soon as I press the "Bearbeiten" button, the view is switched. What is bothering me about this now? Well I have in principle almost the same view twice with QLineEdits, QPlainTextEdit and QListView and so on and would have to add the data also correspondingly about a QDataWidgetMapper with both views.

    Therefore my question: Is there a better way to design such an "edit mode"?

    Thanks a lot for your tips!

  • Lifetime Qt Champion


    Create one editor widget and use it twice.

  • @SGaist
    Thanks for the feedback. If I interpret this correctly I need to create a class in QT Creator e.g. "EditorWidget" and draw my GUI using the designer. At the end I have a .ui, .h and .cpp file. Now I have created a new object with EditorWidget *editor = new EditorWidget in my constructor of my MainWindow.cpp. My MainWindow contains a QStackWidget with a page and in this page a few things are created (see screenshot).


    I have tried the following:

        editor = new EditorWidget;
        ui->stackedWidget->insertWidget(0, editor);  //have tried index from 0 - 10 nothing happend

    But unfortunately my editor widget is not added to page 1.

    What else can I do? I still found this. Do I really have to implement it like this?

  • @Gabber
    That reference is for seeing a custom widget in Qt Designer. I assume your question is about the runtime behaviour when you run your application.

    Your code looks OK (though you should set the current index after you have inserted the widget rather than before).

    Start by checking you can see the EditorWidget OK without putting it on the QStackedWidget. Put some other Qt widget on your QStackedWidget, does that work?

  • @JonB

    If I setCurrentIndex after the insertWidget statement it works. Unfortunately this is not what I want to achieve. A QStackWidget has "pages" (2 by default) in QtCreator and I want to add my editor widget to this page 1 with the designed widgets from MainWindow. How can I do that? Is it possible at all?

  • @Gabber said in What is the easiest way to desing an "edit mode" via my GUI in QtDesigner?:

    If I setCurrentIndex after the insertWidget statement it works.

    Well that's good.

    I don't really know what you mean. If Designer automatically puts 2 pages in for you, and you don't want one of them, and you can't delete it at design-time, you can always delete it at runtime. You can insert your widget as a new page in its place if that's what you mean. OTOH, if you mean you already have a page, perhaps with some widgets, from design-time and you just want to add your widget onto that page, you won't want to be calling ui->stackedWidget->insertWidget().

  • @JonB

    Sorry for my poor explanation. My MainWindwo already contains a created GUI to which I want to add the EditorWidget. Here is a screenshot:

    As you can see on the right, the QStackWidget has a "firstPage". And to this "firstPage" I want to add my EditorWidget. How can I do that?

    Later I have to create a "secondPage" and add the EditorWidget there as well, which works via ui->stackWidget->addWidget(editor).

  • @Gabber
    Something like ui->stackedWidget->firstPage()->modelLayout->addWidget(editor)? [Actually just ui->modelLayout->addWidget(editor) may work.]

    In one case you are adding widget onto an existing page, in the other case you are adding widget as a new page on the stacked widget.

  • @JonB

    Thank you very much for your tip! I have now managed it as follows: ui->firstPage->layout()->addWidget(editor);

    My problem is solved. Thank you

Log in to reply