Qt 在每个 class 中访问查看器 object?
Qt access viewer object in every class?
我的 mainwindow.cpp 中有一个进度条 object (ui->QprogressBar)。但是,我想在另一个 class (readerfile.cpp) 中使用这个 object。
Headers
mainwindow.h
demo.h
来源
mainwindow.cpp
demo.cpp
我大部分时间都用这个方法调用object:- 使用函数调用,例如-mainwindow.cpp 我会调用这个函数
mainwindow->isFunction(ui->QprogressBar);
isFunction 在我的 demo.cpp 文件中可用
void demo :: isfunction (QProgressBar *progress)
但是,现在我想直接在我的 demo.cpp 文件中使用 QprogressBar object。
我尝试了所有可能的组合、连接,但就是无法正常工作。
那么有人可以向我解释一下,如何从 class 演示中访问 UI 元素 object。
任何解决方案的想法都会有很大的帮助。
谢谢。
要从另一个 class 获取指向对象的指针,您需要实现一个 public 函数来 returns 这个指针。我给你举个小例子:
你的头文件中的classMainWindow
会包含一个函数progressbar()
.
mainwindow.h
:
//...
class MainWindow : public QMainWindow
{
Q_ObBJECT
public:
QProgressBar *progressbar(); //returns a pointer to the QProgressBar
//..
private:
//..
};
这个函数在mainwindow.cpp
中是这样实现的:
QProgressBar *MainWindow::progressbar()
{
return ui->progbar; //I just called it like this to avoid confusion, it's the just the name you defined using QtDesigner
}
然后,在 demo.hpp
中,如果您的 class 中有 MainWindow
的实例:
//..
class Demo : public QObject
{
Q_OBJECT
public:
//..
private:
MainWindow *window;
//..
}
您可以通过调用 demo.cpp
:
中的函数访问 QProgressBar
QProgressBar *bar;
bar = window->progressbar();
我不得不说,在另一个 class 中有一个 MainWindow
的实例是不寻常的。通常你的 QMainWindow
或 QApplication
是程序的主要入口点,你有其他 classes 的实例,而不是相反。
我的 mainwindow.cpp 中有一个进度条 object (ui->QprogressBar)。但是,我想在另一个 class (readerfile.cpp) 中使用这个 object。
Headers
mainwindow.h
demo.h
来源
mainwindow.cpp
demo.cpp
我大部分时间都用这个方法调用object:- 使用函数调用,例如-mainwindow.cpp 我会调用这个函数
mainwindow->isFunction(ui->QprogressBar);
isFunction 在我的 demo.cpp 文件中可用
void demo :: isfunction (QProgressBar *progress)
但是,现在我想直接在我的 demo.cpp 文件中使用 QprogressBar object。 我尝试了所有可能的组合、连接,但就是无法正常工作。 那么有人可以向我解释一下,如何从 class 演示中访问 UI 元素 object。
任何解决方案的想法都会有很大的帮助。 谢谢。
要从另一个 class 获取指向对象的指针,您需要实现一个 public 函数来 returns 这个指针。我给你举个小例子:
你的头文件中的classMainWindow
会包含一个函数progressbar()
.
mainwindow.h
:
//...
class MainWindow : public QMainWindow
{
Q_ObBJECT
public:
QProgressBar *progressbar(); //returns a pointer to the QProgressBar
//..
private:
//..
};
这个函数在mainwindow.cpp
中是这样实现的:
QProgressBar *MainWindow::progressbar()
{
return ui->progbar; //I just called it like this to avoid confusion, it's the just the name you defined using QtDesigner
}
然后,在 demo.hpp
中,如果您的 class 中有 MainWindow
的实例:
//..
class Demo : public QObject
{
Q_OBJECT
public:
//..
private:
MainWindow *window;
//..
}
您可以通过调用 demo.cpp
:
QProgressBar
QProgressBar *bar;
bar = window->progressbar();
我不得不说,在另一个 class 中有一个 MainWindow
的实例是不寻常的。通常你的 QMainWindow
或 QApplication
是程序的主要入口点,你有其他 classes 的实例,而不是相反。