简体   繁体   中英

Qt: How to connect class to custom Qt Designer Widget

Maybe I'm thinking about this completely wrong...

I've created a new widget in Qt Creator with a Designer file (I picked the Widget template, which generated a source and header file for my custom widget class, and also a designer file).

I then designed the widget with the Designer. I can now create instances of this widget and it will show up in my app.

But it's not terribly useful because I don't know how to customize the widget at runtime.

Let's say all I've got in the widget is a Label and a Button. At runtime, how can I change the text of this label? I can't figure out how to connect the designer stuff to my actual class, and I can't find any documentation on how to do this. Am I missing something?

Thanks!

A few things:

  • In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. This name is the name of the variable that you can use in C++ to reference that widget and call functions on it.

  • Depending on how your custom widget was implemented, you will be able to reference these variables using one of two methods:

    • If your class inherits from Ui::MyCustomwidget, then your variables are simply member variables of your class and can be accessed at any time (myLabel->setText())

    • If you have a member variable (generally named ui, of type Ui::MyCustomWidget), then you can access your widgets using the ui object (ui->myLabel->setText())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM