.h
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Ui::MainWindowClass ui;
private slots:
void on_pushButton_clicked();
};.cpp
void Test()
{
MainWindow mw;
mw.ui.pushButton->move(QPoint(200, 200));
qDebug() << "test" << "\n";
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
QObject::connect(shortcut, &QShortcut::activated, this, &Test);
return;
}如何从函数ui中的MainWindow类访问Test的内容,而不使Test成为子/继承MainWindow
我认为我所做的不是像用快捷方式调用函数时那样工作,所以按钮没有移动。
发布于 2022-10-01 17:00:04
Qt将ui文件编译为ui_.h (您可以在构建文件夹中看到它们)。所以您可以使用ui,只需包含ui_.h
示例
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Ui::MainWindow *ui;
private:
};
#endif // MAINWINDOW_Hmainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}main.cpp
#include "mainwindow.h"
#include <QApplication>
#include "ui_mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.ui->progressBar->setValue(30);
return a.exec();
}https://stackoverflow.com/questions/73916520
复制相似问题