QT子类调用父类的ui

首先,子类要包含父类的头文件和ui头文件

然后,通过构造的方式把父类指针传递给传递给子类*(最优方法,其他的也可以)*

#ifndef MEWEB_H
#define MEWEB_H
//首先
#include <mexplore.h>		
#include "ui_mexplore.h"	

class MEweb
{
public:
    MEweb(MExplore *m_parent);	//然后
};

#endif // MEWEB_H

再然后,在父类中声明父类的ui变量为公有*(或友元)*

public:
    Ui::MExplore *ui;

接着,main函数里面也要声明子类的成员(声明即调用构造函数),将父类的成员的地址传给子类成员。并且子类的声明一定要在父类之后。

MExplore w;
w.show();
MEweb s(&w);

这样,我们就可以直接在子类中调用父类的指针里面的ui

m_parent->ui->urlLine->setText("12346");
  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以在类中定义一个QGraphicsView控件,并且将其继承Ui_MainWindow中的QGraphicsView控件,然后在类中重写一些方法来实现图片显示、放大缩小等功能。 下面是一个简单的示例代码: ```python from PyQt5.QtWidgets import QMainWindow, QGraphicsView from PyQt5.QtCore import Qt from PyQt5.QtGui import QImage, QPixmap from ui_mainwindow import Ui_MainWindow class MyGraphicsView(QGraphicsView): def __init__(self, parent=None): super(MyGraphicsView, self).__init__(parent) self.setDragMode(QGraphicsView.ScrollHandDrag) def set_image(self, image): scene = self.scene() if scene is None: scene = QGraphicsScene(self) self.setScene(scene) pixmap = QPixmap.fromImage(image) scene.clear() scene.addPixmap(pixmap) self.fitInView(pixmap.rect(), Qt.KeepAspectRatio) def wheelEvent(self, event): delta = event.angleDelta().y() factor = 1.1 ** (delta / 240.0) self.scale(factor, factor) class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.graphicsView = MyGraphicsView(self.centralwidget) # 将类的graphicsView控件继承类的graphicsView控件 self.graphicsView.setObjectName("graphicsView") self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1) def show_image(self, image_path): image = QImage(image_path) self.graphicsView.set_image(image) ``` 在这个示例中,我们定义了一个类`MyGraphicsView`,它继承了类`Ui_MainWindow`中的`QGraphicsView`控件,并且重写了`set_image`方法和`wheelEvent`方法实现了图片显示、放大缩小等功能。在`MainWindow`类中,我们将类`MyGraphicsView`的对象作为类`Ui_MainWindow`中的`QGraphicsView`控件来使用,这样就可以直接调用`set_image`方法来显示图片,并且可以通过`wheelEvent`方法实现放大缩小功能。 你可以参考这个示例代码,在自己的项目中进行修改和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值