开放 Qt窗口的 this指针 的方法汇总 (*****)

目录

开放 Qt窗口的 this指针 : 静态成员变量法(ok):静态指针成员变量法。

主窗口指针传递给子窗口:构造函数法

主窗口指针传递给子窗口:信号槽法,优点:不局限于主子窗口之间的传递?

Qt之如何获取主窗口的指针:可用于开发插件

官方库函数:parentWidget()

----------------------------------------------

问题:

使用指针的麻烦:在代码运行使用之前,必须保证其中的数据有效。。

在同级类之间进行指针传递,使用信号槽,要繁琐一些,但是,应该没有上述的麻烦事?

------

参考:

QT---获取主窗口指针

QT---获取主窗口指针_qt获取mainwindow指针_VS,路在脚下的博客-CSDN博客

MainWindow *ptr = NULL;
ptr = (MainWindow*) parentWidget(); 
       
//在主窗口中,带this实例化子窗口,之后在子窗口中获取主窗口的this。

官方库函数:parentWidget()

QWidget *QWidget:: parentWidget() const

Returns the parent of this widget, or nullptr if it does not have any parent widget.

----------------------------------------------------

参考:

QVariant 存取任意类型的指针 、自定义数据类型(****)
https://blog.csdn.net/ken2232/article/details/131733264

开放 Qt窗口的 this指针 的方法汇总 (*****)
https://blog.csdn.net/ken2232/article/details/129634701

===========================

开放 Qt窗口的 this指针 : 静态成员变量法(ok)

参考: Qt中静态变量使用方法_qt静态变量_xiaoazhang0的博客-CSDN博客

  1. abc.h

class Abc : public QWidget
{
    Q_OBJECT
public:
    explicit Abc(QWidget *parent = nullptr);
    ~RunTable();

    static Abc * pGetAbcThis; // 1. 首先定义本窗口类的静态指针成员。用于存储本窗口的this

 
2. abc.cpp

Abc * Abc::pGetAbcThis(nullptr);  // 2. 实例化。前一个Abc是类类型,后一个是类作用域
Abc * Abc::pGetAbcThis=nullptr; // C赋值风格,上句是c++赋值风格

Abc::Abc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Abc)
{
    ui->setupUi(this);

    pGetAbcThis = this;  // 3. 赋值

 
3. 引用

在任何需要引用的cpp中

首先需要包含该头文件


#include "abc.h"

其次,由于需要访问的是窗口的ui部件,所以还需要包含


#include "ui_abc.h"

使用

Abc::pGetAbcThis->ui->....  //

注意:

1. static成员变量:不能在头文件中初始化

//3.static成员必须在类外初始化;并且,不能在头文件中初始化;否则,在链接时可能会出现重定义的问题。

2. 问题:将指针定义为静态成员时。定义为全局变量,似乎也不错。<<不同的应用场景。

2.1. 当一个类实例化 2个、或以上的对象时,这个静态指针,到底指向了哪个对象的 this?

需要自己考虑。

好处:总可以保证实例化之后,才使用变量。

2.2. 定义为全局变量,可以做到一个指针变量,对应于一个对象。

有时难以控制。

程序员A确保了 静态成员 this指针的唯一性。

结果程序员B有用该类实例化了一个新对象,这时就可能有问题了?

坏处:在未实例化就使用变量,则、错误信息:The program has unexpectedly finished.。

==========================

主窗口指针传递给子窗口:信号槽法

输出结果:

receivethis.cpp in slot, receive_sendThisChanged_slot() : sendThis->data : "This is the data in sender"
receivethis.cpp, receiver get the sender's (sendThis) : SendThis(0x8bfca0)
sendthis.cpp, the sender's : this->data : "This is the data in sender"
sendthis.cpp, the sender's (this) : SendThis(0x8bfca0)

sendthis.h

#ifndef SENDTHIS_H
#define SENDTHIS_H

#include <QObject>
#include <QString>

#include "receivethis.h"

class SendThis : public QObject
{
Q_OBJECT

public:
explicit SendThis(QObject *parent = nullptr);
~ SendThis();

signals:
//void sendThis(SendThis *sendThis); //原作者代码

void sendThis_signal(SendThis *sendThis); //step 1: 重要步骤

public:
QString data = "This is the data in sender";

};

#endif // SENDTHIS_H

sendthis.cpp

#include "sendthis.h"
#include "receivethis.h"

#include <QCoreApplication>
#include <QThread>
#include <QDebug>

SendThis::SendThis(QObject *parent) : QObject(parent)
{
qWarning() << "gui current thread ID:" << QThread::currentThreadId() << '\n';

  
// 指针传递方式一
ReceiveThis *worker = new ReceiveThis(this); //step 2:

 
// 指针传递方式二
connect(this, &SendThis::sendThis_signal, worker, &ReceiveThis::receive_sendThisChanged_slot); //step 3:

emit sendThis_signal(this); //step 4:

qDebug()<<"sendthis.cpp, the sender's : this->data : "<<this->data;
qDebug()<<"sendthis.cpp, the sender's (this) : "<<this;
}

SendThis::~SendThis()
{

}

receivethis.h

#ifndef RECEIVETHIS_H
#define RECEIVETHIS_H

#include <QObject>
#include "sendthis.h"

class SendThis; //step 5:

class ReceiveThis : public QObject
{
Q_OBJECT

public:
explicit ReceiveThis(SendThis *sendThis = nullptr);
~ ReceiveThis();

public slots:
void receive_sendThisChanged_slot(SendThis *sendThis); //step 6:

};

#endif // RECEIVETHIS_H

receivethis.cpp

#include "receivethis.h"
#include <QDebug>

ReceiveThis::ReceiveThis(SendThis *sendThis) : QObject(sendThis) //step x1 : QObject(sendThis)
{
sendThis->data; // 原作者的代码,"This is the data in sender"
//qDebug()<<"receivethis.cpp in ReceiveThis(): sendThis->data : "<<sendThis->data; //程序异常结束。
}

ReceiveThis::~ ReceiveThis()
{

}

void ReceiveThis::receive_sendThisChanged_slot(SendThis *sendThis) //step 7:
{
sendThis->data; // "This is the data in sender"

qDebug()<<"receivethis.cpp in slot, receive_sendThisChanged_slot() : sendThis->data : "<<sendThis->data;
qDebug()<<"receivethis.cpp, receiver get the sender's (sendThis) : "<<sendThis;
}

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

ReceiveThis teatreceiveThis;
SendThis teatSendThis;
}

参考:

QT两个类之间*指针传递

QT两个类之间*指针传递_qt不同类之间通过指针传递_于大博的博客-CSDN博客

==========================

主窗口指针传递给子窗口:构造函数法

参考:

开放 Qt窗口的 this指针 的方法汇总 (*****)

http://t.csdn.cn/f83p4

Qt 子对象引用mainwindow指针的巧妙方法 (***)

http://t.csdn.cn/N8cbi

==========================

Qt之如何获取主窗口的指针:可用于开发插件


QMainWindow* getMainWindow()
{
    foreach (QWidget *w, qApp->topLevelWidgets())
        if (QMainWindow* mainWin = qobject_cast<QMainWindow*>(w))
            return mainWin;
    return nullptr;
}

Qt之如何获取主窗口的指针_qt在析构时如何mainwindow指针_草上爬的博客-CSDN博客

Qt 中 母窗口的指针传给子窗口

比较:子窗口是游离的,实现比“在子窗口中,直接获得母窗口的指针”要复杂。

问题是:下面这些方法,与具体的实现环境有关,在有些程序结构中是无效的。

pGetAbcThis = (MainWindow *)parent;

pGetCdeThis = new Config((void *)this);

问题参考:Qt 子对象引用mainwindow指针的巧妙方法

可能需要修改构造函数,有些构造函数的修改是简单的,但有些是复杂的。

Qt 子对象引用mainwindow指针的巧妙方法

https://www.cnblogs.com/jiguang321/p/11692736.html

问题描述:最近在用qt开发项目的时候,有一个需要就是在子类中调用mainwindow指针,在mainwindow上显示内容。

因为在mainwindow中需要生成子类的对象,所以不能在子类中引用mainwindow的头文件,这样会造成相互包含报错!

后来查到大家推荐使用parentWidget这个方法,然而在我的项目中该函数返回的却是一个nullptr,

在子窗口中,直接获得母窗口的指针

Qt官方手册


QWidget *QWidget::parentWidget() const

Returns the parent of this widget, or nullptr if it does not have any parent widget.

在母窗口的 cpp中


Form *xx = new Form(this);

在子窗口(当前窗口中)的cpp:


Form *ptr = (Form*)parentWidget();
ptr->show();

局限:子窗口被嵌入母窗口中。

Form *xx = new Form;  // 不带this,总是返回空指针)

返回信息: Form QWidget(0x0)

QT 子窗口获取父窗口指针的方法

qobject_cast<PhoenixDemo*>(parentWidget())->ui.actionPlayStop->setDisabled(true); 

QT 子窗口获取父窗口指针的方法_qt获取父窗口指针_手牵手的博客-CSDN博客

参考:

QT中主窗口的指针传给子窗口_qt中的父窗口指针传递给子窗口_King-超的博客-CSDN博客

Qt:子窗口中如何获取主窗体ui中的控件

Qt:子窗口中如何获取主窗体ui中的控件_百度知道

在自定义的imageWidget中获取MainWindow的widget?如:pen.setWidth(spinbox->value());

答:

  1. 不建议通过这种方式,这样会破坏类的封装性!建议在主窗口中定义接口函数,在接口函数中可以访问主窗口的任何控件。然后在子窗口中parentWidget()取得主窗口的指针,使用该指针调用主窗口的接口函数,从而访问主窗口的控件。

  1. 通过类的访问或者继承均可

QT中怎么在一个窗口中获得另一个窗口的指针?

QT中怎么在一个窗口中获得另一个窗口的指针?--CSDN问答

在widget中连接dialog信号和widget槽

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值