传递带指针的QObject(Qt)
Passing QObject with Pointer (Qt)
我的目标是将windowobject指针传递给另一个类。我会向你展示我目前为止的成果。其中:"对话框"是要通过的窗口。
主窗口.cpp
dialog = new Dialog(this);
someClass(dialog);
某类中的Konstruktor
someClass::someClass(Dialog *d)
{
Dialog *dia = d;
}
someClass.h
#include "dialog.h"
...
public:
someClass(Dialog *dialog)
//Dialog d;
这个程序现在运行了,但我不确定我是否达到了我想要的。现在可以与我的对话框交互吗?我想要的是这样的东西。
dia->ui->lineEdit->setText();
任何帮助都将通知
someClass(&dialog);
不正确。。。您有一个指针,并在函数中给出指针的地址(指向指针的指针)
你也有
Dialog d;
并为其分配一个Dialog*
。
我建议你看一看:最终C++图书指南和列表
我的目标是将windowobject指针传递给另一个类。我会的让你看看我到目前为止得到了什么。其中:"对话框"是要通过的窗口。
考虑您的代码:
someClass::someClass(Dialog *d)
{
Dialog *dia = d;
}
是构造函数someClass中的本地成员。因此,它只在构造函数本身中具有作用域(在构造函数之外不可见,事实上,它不存在于构造函数之外(当构造函数超出作用域时被销毁))。
幸运的是,dia是一个指针(对象的地址),而不是实际的对话框(因此只有指针,而不是它所指向的对象超出了范围)。如果您希望指针保留在作用域中以便以后访问,则必须将其"绑定"到类的作用域(使其成为类成员)。
class MyClass
{
public:
//Using reference as it may not be null...
MyClass( Dialog& dialog );
void showDialog();
private:
//We only want to expose a small part of dialog to users,
// hence keep it private, and expose what we want through
// the interface (the public part).
Dialog& dialog_;
};
//MyClass.cpp
MyClass::MyClass( QPointer<Dialog> )
: dialog_( dialog ) //Google "member initialisation"
{
}
void MyClass::showDialog(){ dialog_.show(); }
-----修改/附加答案----
如果在上面的示例中dialog_是可选的,那么您不必将其作为引用成员,因为引用成员需要初始化(不能有未初始化的引用)。在这种情况下,让它成为一个指针。。。当使用Qt时,我会将其设为QPointer(假设Dialog是QObject),因为QPointer比原始指针更安全(至少它们总是初始化为零)。
我将向您展示目前保持简单的基本原则。阅读有关QPointer和智能指针的一般信息。
例如:
class MyClass
{
public:
// May or may not hold zero...
explicit MyClass( Dialog* dialog = 0 );
void showDialog();
private:
//We only want to expose a small part of dialog to users,
// hence keep it private, and expose what we want through
// the interface (the public part).
Dialog* dialog_;
};
//.cpp
MyClass::MyClass( Dialog* dialog /* = 0*/ )
: dialog_( dialog )
{
}
void MyClass::showDialog()
{
if( dialog_ )
{
dialog_->show();
}
else
{
std::cout << "This is in fact not a dialog"
"nbut be so kind as to enter"
" whatever you want here ;-)"
<< std::endl;
while( !terminated() )
{
std::string inputStr;
std::cin >> inputStr;
evalute( inputStr );
}
}
}
我们不知道你的Dialog
类是什么样子的,但如果它的成员ui
是公共的(或者someClass
是Dialog
的朋友),那么你应该能够进行
dia->ui->lineEdit->setText();
你有编译器错误吗?还是文本根本没有按预期显示?您仍然需要在某个时刻使用显示对话框
dia->show();
或
dia->exec();
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- QObject::d eleteLater在我的Qt测试中没有像预期的那样调用
- 具有 QObject 继承的单例 - Qt
- QT 库 QObject::connect: 无法连接 (null)::done() 到 MainWindow::upd
- Qt Quick + CMake + 自定义 QObject 导致对"vtable"的未定义引用
- 如何列出从Qt中Qobject继承的类的所有成员变量
- 删除 QObject 后 Qt 信号仍在触发
- .Qt :阻止 QObject 接收信号
- QObject::连接:QT上没有这样的插槽
- 如何在连接 lambda 时将 Qt::ConnectionType 传递给 QObject::connect
- Qt-Qml连接到上下文属性的QObject属性的信号
- 如何<double>从基于 QObject 的类方法返回 QList 以在 Qt 脚本中使用
- QT 请求"QObject::connect: No such slot BackEnd::RequestReceived"
- 如何通过QT中的参考为qobject进行分配
- 从另一个(非 qt)线程调用 QObject 方法的线程安全性?
- Qt QObject dynamic array
- Qt 是否可以安排在 QObject 被销毁时将 QObject* 设置为 nullptr
- Qt - 堆栈上具有父级的 QObject 如何被删除两次?
- 无法连接Qt QObject::connect()函数
- c++ qt QObject:: connect: No such slot