QT中this指针与成员指针ui访问控件的差异疑问
this access Qt UI controls while ui pointer can? Great question—this is super common when you're just getting started with Qt's UI Designer! Let's break down exactly why this can't reach those controls, but your ui pointer can.
First, let's recall how Qt's UI Designer works
When you drag-and-drop controls like horizontalSlider or progressBar in the Designer, Qt automatically generates a hidden header file (usually ui_mywindow.h) that defines the Ui::MyWindow class. This class has direct member pointers for every control you added in the Designer. Think of it as a "wrapper" that holds all your UI elements in one organized place.
Your MyWindow class vs. the Ui::MyWindow class
Your MyWindow class inherits from QMainWindow, but it doesn't have any of those UI controls declared as its own members. The only UI-related thing it has is a single member pointer:
Ui::MyWindow *ui;
When you call ui->setupUi(this) in the constructor, two key things happen:
- It creates all the UI controls you designed in the Designer.
- It sets
this(yourMyWindowinstance) as the parent widget for all those controls.
But even though the controls are child widgets of MyWindow, MyWindow itself doesn't have direct variables pointing to them. The ui pointer is the only way to get direct, type-safe access to those control pointers—because they're members of the Ui::MyWindow class, not your MyWindow class.
What if you really wanted to use this to access them?
You technically could use Qt's findChild method to locate the controls through this, like this:
QSlider* slider = this->findChild<QSlider*>("horizontalSlider"); if (slider) { // Do something with slider }
But this is not recommended: it's slower, not type-safe (you have to get the control's object name exactly right), and defeats the purpose of using the Designer's generated ui class, which gives you clean, direct access.
To sum up
thispoints to yourMyWindowinstance, which inheritsQMainWindowbut doesn't have the UI controls as direct members.uipoints to an instance of the Designer-generatedUi::MyWindowclass, which does have direct pointers to every UI control you added.
That's why ui->horizontalSlider works, but this->horizontalSlider doesn't!
Your Code for Reference
my_window.h:
#ifndef MY_WINDOW_H #define MY_WINDOW_H #include <QMainWindow> namespace Ui { class MyWindow; } class MyWindow : public QMainWindow { Q_OBJECT public: explicit MyWindow(QWidget *parent = nullptr); ~MyWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::MyWindow *ui; }; #endif // MY_WINDOW_H
my_window.cpp:
#include "my_window.h" #include "ui_mywindow.h" MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyWindow) { ui->setupUi(this); connect(ui->horizontalSlider,SIGNAL(valueChanged(int)), ui->progressBar,SLOT(setValue(int))); } MyWindow::~MyWindow() { delete ui; } void MyWindow::on_pushButton_clicked() { ui->label->setText("Clicked!"); } void MyWindow::on_pushButton_2_clicked() { ui->label->setText("Why...?"); }
内容的提问来源于stack exchange,提问作者KDY

