当前位置: 首页 > news >正文

广西公司做网站网站建设先修课程

广西公司做网站,网站建设先修课程,html5公司网站欣赏,佛山三水区有没有网站建设公司前言 QT创建窗体工程#xff0c;一般在MainWindow或Dialog类里可以直接通过ui指针访问控件#xff0c;但是添加新的类后又如何访问呢#xff0c;可以通过以下几种方式#xff1a; 将ui指针公开后直接访问 #xff08;1#xff09;例如有个自己定义的类CustomCl…   前言         QT创建窗体工程一般在MainWindow或Dialog类里可以直接通过ui指针访问控件但是添加新的类后又如何访问呢可以通过以下几种方式 将ui指针公开后直接访问 1例如有个自己定义的类CustomClass在自定义类里包含主界面指针MainWindow * class MainWindow;   class CustomClass { public:     CustomClass(MainWindow * parent);     MainWindow * mainwidow;    void SetUI(); }; 2主界面类将成员Ui::MainWindow *ui 从私有private移动到public公共 class MainWindow : public QMainWindow {     Q_OBJECT      public:     MainWindow(QWidget *parent nullptr);     ~MainWindow();     Ui::MainWindow *ui;     CustomClass* customclass; private:    } 3自定义类包含头文件#include ui_mainwindow.h构造的时候传入界面指针MainWindow*就能通过 mainwidow-ui指针访问UI控件了。 #include mainwindow.h #include ui_mainwindow.h   CustomClass::CustomClass(MainWindow * parent) {   this-mainwidow parent; }   void CustomClass::SetUI() {     mainwidow-ui-pushButton-setText(开始); } 记得要引用ui_mainwindow.h不然会报错误 error: member access into incomplete type Ui::MainWindow forward declaration of Ui::MainWindow 封装成公共函数 1所有对UI的操作都在主界面MainWindow类中并封装成公共的函数 class MainWindow : public QMainWindow {     Q_OBJECT      public:     MainWindow(QWidget *parent nullptr);     ~MainWindow();      void SetUI();     CustomClass* customclass; private:            Ui::MainWindow *ui; }   void MainWindow::SetUI() {     this-ui-pushButton-setText(开始); } 2其他类要访问UI调用函数就好了 CustomClass::CustomClass(MainWindow * parent) {   this-mainwidow parent;   this-mainwidow-SetUI(); } 通过控件指针访问 如果每次只访问一两个控件的话也可以直接将控件指针传给自定义类customclassnew CustomClass(this);     customclass-SetUI(ui-pushButton); void CustomClass::SetUI(QPushButton* btn) {     btn-setText(开始); } 通过信号和槽访问 前面的方法一般够用了但如果是多线程就必须用到信号和槽机制因为非UI线程不能跨线程访问UI例如定义一个线程类 class MyThread :public QThread {     Q_OBJECT public:     MyThread(MainWindow *parent);     MainWindow * mainwidow;     void run() override; }; 在主界面MainWindow类里有信号setui和槽函数SetUI所有对 UI的操作都封装在槽函数函数中 MainWindow::MainWindow(QWidget *parent)     : QMainWindow(parent)     , ui(new Ui::MainWindow) {     ui-setupUi(this);     //关联信号      connect(this,MainWindow::setui,this,MainWindow::SetUI);      mythread new MyThread(this);      mythread-start();//启动线程 }   void MainWindow::SetUI() {     this-ui-pushButton-setText(开始); } 在非UI线程里需要访问Ui通过发送信号就行了槽函数会在UI线程中被执行 void MyThread::run() {     //发送信号修改UI     emit this-mainwidow-SetUI();     exec(); } 当然信号和槽很灵活不一定在多线程中有需要都可以用。 /**************************************** 在子线程里控制主界面的UI控件有两种方法第一种是在子线程中发送信号然后在主线程中去更新第二种方法是在子线程中创建同样的对象然后把主界面中控件的指针赋给创建的对象。 第一种方法在此不做实例展示在此通过一个简单的例子展示第二种方法 下面是主界面的初始转态 下面这个是继承自QThread类的子线程类 sonthread.h #ifndef SONTHREAD_H #define SONTHREAD_H #include QLabel #include QThread #include QDebug class sonThread : public QThread {     Q_OBJECT public:     explicit sonThread(QObject *parent nullptr);     void run(); public:     QLabel *label; }; #endif // SONTHREAD_H sonthread.cpp #include sonthread.h sonThread::sonThread(QObject *parent) : QThread(parent) {     label new QLabel; } void sonThread::run() {     qDebug()run()QThread::currentThreadId();          label-setText(更新); } 下面是主线程类 dialog.h #ifndef DIALOG_H #define DIALOG_H #include QDialog #include QThread #include sonthread.h namespace Ui { class Dialog; } class Dialog : public QDialog {     Q_OBJECT public:     explicit Dialog(QWidget *parent 0);     ~Dialog(); private:     Ui::Dialog *ui; }; #endif // DIALOG_H dialog.cpp #include dialog.h #include ui_dialog.h Dialog::Dialog(QWidget *parent) :     QDialog(parent),     ui(new Ui::Dialog) {     ui-setupUi(this); sonThread *sonthread new sonThread;  //创建子线程对象     sonthread-labelui-label;  //将主界面UI指针赋给子线程中的指针对象     sonthread-start();  //启动子线程 qDebug()Dialog()QThread::currentThreadId(); } Dialog::~Dialog() {     delete ui; } 下面是运行结果 可以看出run()函数与主线程不在同一个线程而我只在run()中有修改过label的字符所以实现了在子线程中操作主界面UI控件的目的。
http://wiki.neutronadmin.com/news/325466/

相关文章:

  • 上海网站建设高端定制网络服务公司gzip 网站
  • 企业建站服务退役军人专业做logo的网站
  • 网站建设 技术网站建设技术问题
  • 开江建设局网站springcloud项目搭建
  • 旅游网站建设的总结专业移动微网站设计
  • wordpress子目录多站点设置杭州哪家做网站比较好
  • 淘宝的电子商务网站的建设龙华新区网站制作
  • discuz!网站模板杭州网站建设案例
  • 深圳做网站 信科网络手工制作大全视频教程
  • 网站首页包含的内容地方门户网站设计
  • 最快做网站的语言怎么找到php网站的首页面html
  • 代码做网站常用单词重庆网站建设 菠拿拿
  • 王晴儿网站建设方案wordpress iis 中文
  • 怎么利用花生壳做自己的网站帮客户做网站的公司
  • 网站建设费的会计处理摄影作品可以在哪些网站投稿
  • 怎么看别人网站是什么语言做的无锡网站制作网站
  • 建站快车是什么wordpress博客增加音乐页面
  • 网站设计论文范文大全集有哪些做室内设计好用的网站
  • 湘潭电大网站下列关于网站开发中
  • 网站建设费用报价wordpress文章id
  • 做网站像美团一样多少钱揭阳做网站哪个好
  • 用vs2008做网站本科学院网站建设方案
  • 在哪里能建免费的网站做水果生意去那个网站
  • 建站宝盒的设置wordpress 如何删除所有评论
  • 盐城大丰网站建设找哪家好wordpress购物模板下载
  • 加强 廉政网站建设超级seo工具
  • 英文网站建设技术中国品牌vi设计公司
  • 网站上怎么做全景看图广元做网站站排名
  • 郑州做网站推广地址wordpress 网站logo
  • 企业做电商网站上海建设部网站