Important: Please read the Qt Code of Conduct -

Widget Ownership

  • Just starting so checking a thing:
    If I create an widget in mainwindow then mainwindow will normally own it and I can refer to it as eg plainTextEdit within mainwindow's functions. Yes?
    If I create a widget via the designer then it would seem that the ui owns it and I always have to refer to it as ui->plainTextEdit. Is that right? It seems a bit cumbersome. Perhaps I've got it wrong. Perhaps there's a better way to do things. Perhaps there's some kind of shortcut or reference mechanism. I am a bit puzzled.

  • Lifetime Qt Champion


    When creating a complex widget using Designer, yes, you have to use ui->plainTextEdit since that class is generated from the XML built by Designer.

    Is it cumbersome ? It's rather a question of whether you like to build your UI using Designer or do everything by hand. No, there's no shortcut to avoid ui->something when you want to do something with an element of that widget.

    If that's something you really don't like it, then you have the alternative to write your own widget completely by hand. It really is just a matter of taste.

  • Thanks, that's all I needed to know. It means I've got the right idea and can proceed. What I will probably do is to create a reference to such objects within mainwindow eg QPlainTextEdit *PTEd = ui->plainTextEdit, and use that.

  • Lifetime Qt Champion

    Frankly, that will just add clutter to your code and make it less understandable when you (or someone else) re-read it later.

  • OK, I take your point. I guess I'm used to dear old BCB6 where that didn't happen. It's a paradigm I'm not familiar with and after all it only needs a bit of copy/paste, doesn't it. And it will ensure that I know where objects come from. So, yeah.

Log in to reply