Qt访问每个类中的查看器对象?
Qt access viewer object in every class?
我的主窗口中有一个进度条对象(ui->QprogressBar(.cpp。但是,我想在另一个类(readerfile.cpp(中使用此对象。
头
主窗口.h
演示.h
来源
主窗口.cpp
演示.cpp
我大部分时间都使用此方法调用对象:- 使用函数调用,例如 -mainwindow.cpp我将调用此函数
mainwindow->isFunction(ui->QprogressBar);
isFunction 在我的演示.cpp文件中可用
void demo :: isfunction (QProgressBar *progress)
但是,现在我想直接在我的演示.cpp文件中使用 QprogressBar 对象。 我尝试了所有可能的组合,连接,但无法使其工作。 所以有人可以解释一下,如何从类演示访问 UI 元素对象。
任何解决方案的想法都将是一个很大的帮助。 谢谢。
要从另一个类获取指向对象的指针,您需要实现一个返回此指针的公共函数。我举个小例子:
头文件中的类MainWindow
将包含一个函数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
}
然后,如果您的类中有MainWindow
实例,则demo.hpp
:
//..
class Demo : public QObject
{
Q_OBJECT
public:
//..
private:
MainWindow *window;
//..
}
您可以通过调用demo.cpp
中的函数来访问QProgressBar
:
QProgressBar *bar;
bar = window->progressbar();
我不得不说,在另一个类中有一个MainWindow
实例是不寻常的。 通常,您的QMainWindow
或QApplication
是程序的主要入口点,并且其中有其他类的实例,而不是相反。
相关文章:
- 按基类对象访问派生类资源时出错
- 使用基类对象访问派生的仅类方法
- 从 const 对象访问非 const 方法
- 如何在C++中使用类对象访问指针数据成员
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 使用二进制搜索树中的迭代器对象访问左侧节点
- 为什么引用类型在使用临时对象访问时是左值
- 从父对象 c++ 访问子方法
- 无法通过指针或对象 c++ 访问受保护的成员
- 为什么可以从类中的对象访问类的私有变量
- 对类似"struct {double, int}"对象使用reinterpret_cast进行对象访问
- 通过无符号 char 别名进行对象访问,加载和存储时会发生什么?
- 从对象访问结构枚举
- 如何全局创建对象并使用该对象访问全局范围内的公共成员函数
- 在C 中,可以从第三个共享对象访问其他两个不同共享对象
- 如何在C 中提供一个可呼叫的对象访问,例如lambda
- C 向量对象访问
- C++是否可以从同级第二个派生对象访问第一个派生对象的受保护基础数据成员?
- 如何从类内实例化的对象访问私有变量
- 多态性:通过类文本或对象访问静态成员