实现效果

点击成员列表中的头像,实现对应对话框弹出的功能。
打开的对话框不可以再次打开,同时弹出提示消息框。

打开一个窗口

再次打开该窗口

补充后的代码

myDialog.cpp文件中新增信号槽处理函数。
myDialog.h文件中新增窗口打开状态的标识符变量。
myWindow.cpp文件中重载窗口关闭函数。closeEvent();
myWindow.h文件中新增窗口关闭信号。

myDialog.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QWidget>
#include <QToolButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QList>
#include <myWindow.h>class MyDialog : public QWidget
{Q_OBJECTpublic:MyDialog(QWidget *parent = nullptr);~MyDialog();private:QToolButton *textButton;QVBoxLayout *vLayout;QHBoxLayout *hLayout;QToolButton *userButton[9];QList<QString> nameList;QList<QString> picList;**QVector<bool> openState;**
};
#endif // MAINWINDOW_H

myDialog.cpp

#include "myDialog.h"
#include "myWindow.h"
#include <QPixmap>
#include <QIcon>
#include <QVector>
#include <QMessageBox>
#include <QDebug>MyDialog::MyDialog(QWidget *parent): QWidget(parent)
{this->setGeometry(200, 400, 250, 700);this->setWindowTitle("MyQQ 2020");textButton = new QToolButton();textButton->setText("群成员");textButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);vLayout = new QVBoxLayout(this);vLayout->addWidget(textButton);vLayout->setContentsMargins(0, 0, 0, 680);nameList << "nil" << "yingge" << "旧日暮里"<< "戏班保洁" << "环球要饭集团"<< "财务"<< "﹏沐.唐ゞ" << "萌新2号" << "森の妖精";picList << ":/img/1.jpg" << ":/img/2.jpg" << ":/img/3.jpg"<< ":/img/4.jpg" << ":/img/5.jpg" << ":/img/6.jpg"<< ":/img/7.jpg" << ":/img/8.jpg" << ":/img/9.jpg";QVector<QToolButton *> vToolButton;for(int i=0; i<9; i++){userButton[i] = new QToolButton();userButton[i]->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);userButton[i]->setText(nameList[i]);userButton[i]->setAutoRaise(true);userButton[i]->setIcon(QPixmap(picList[i]));userButton[i]->setIconSize(QSize(50, 50));vToolButton.push_back(userButton[i]);**openState.push_back(false);**vLayout->addWidget(userButton[i]);}**for(int i=0; i<vToolButton.size(); i++){connect(vToolButton[i], &QToolButton::clicked, [=](){if(openState[i] == true){QString str =QString("%1窗口已经被打开了").arg(vToolButton[i]->text());QMessageBox::warning(this, "warning", str);return ;}myWindow *userWindow = new myWindow();userWindow->setWindowTitle(vToolButton[i]->text());userWindow->setWindowIcon(vToolButton[i]->icon());openState[i] = true;userWindow->show();connect(userWindow, &myWindow::closeMyWindow, [=](){qDebug() <<"hello" <<endl;openState[i] = false;});});}**
}MyDialog::~MyDialog()
{
}

myWindow.h

#ifndef WINDOW_H
#define WINDOW_H#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QTextBrowser>
#include <QTextEdit>
#include <QPushButton>
#include <QSpacerItem>
#include <QComboBox>
#include <QSpinBox>
#include <QFontComboBox>
#include <QTableWidget>class myWindow: public QWidget
{Q_OBJECTpublic:myWindow(QWidget *parent = nullptr);~myWindow();private:QTextBrowser * msgBrowser;QTextEdit * msgTextEdit;QFontComboBox *fontBox;QSpinBox *fontSizeBox;QPushButton *underlineBn;QPushButton *boldBn;QPushButton *saveBn;QPushButton *clearBn;QPushButton *colorBn;QPushButton *tiltBn;QPushButton *sendBn;QPushButton *exitBn;QLabel *onlineNum;QSpacerItem *spacer[2];QTableWidget *tableWidget;QHBoxLayout *hLayout[3];QVBoxLayout *vLayout;void paintEvent(QPaintEvent *event);**signals:void  closeMyWindow();
public :void closeEvent(QCloseEvent *event);**
};#endif // WINDOW_H

myWindow.cpp

#include "myWindow.h"
#include <QFrame>
#include <QPainter>
#include <QPaintEvent>
#include <QSpacerItem>myWindow::myWindow(QWidget *parent): QWidget(parent)
{this->setGeometry(500, 500, 730, 450);msgBrowser = new QTextBrowser();msgTextEdit = new QTextEdit();fontBox = new QFontComboBox();fontSizeBox = new QSpinBox();underlineBn = new QPushButton();boldBn = new QPushButton();saveBn = new QPushButton();clearBn = new QPushButton();colorBn = new QPushButton();tiltBn = new QPushButton();sendBn = new QPushButton();exitBn = new QPushButton();onlineNum = new QLabel();tableWidget = new QTableWidget(0, 1);msgTextEdit->setMaximumHeight(100);boldBn->setMaximumSize(33, 32);boldBn->setIconSize(QSize(26, 26));boldBn->setCheckable(true);boldBn->setIcon(QPixmap(":/img/bold.png"));underlineBn->setMaximumSize(33, 32);underlineBn->setIconSize(QSize(26, 26));underlineBn->setCheckable(true);underlineBn->setIcon(QPixmap(":/img/underline.png"));saveBn->setMaximumSize(33, 32);saveBn->setIconSize(QSize(26, 26));saveBn->setCheckable(true);saveBn->setIcon(QPixmap(":/img/save.png"));clearBn->setMaximumSize(33, 32);clearBn->setIconSize(QSize(26, 26));clearBn->setCheckable(true);clearBn->setIcon(QPixmap(":/img/clear.png"));colorBn->setMaximumSize(33, 32);colorBn->setIconSize(QSize(26, 26));colorBn->setCheckable(true);colorBn->setIcon(QPixmap(":/img/color.png"));tiltBn->setMaximumSize(33, 32);tiltBn->setIconSize(QSize(26, 26));tiltBn->setCheckable(true);tiltBn->setIcon(QPixmap(":/img/tilt.png"));sendBn->setText("发送");exitBn->setText("退出");onlineNum->setText("当前在线人数:0人");tableWidget->setMinimumWidth(180);
//    tableWidQStringList header;header << "用户名";tableWidget->setHorizontalHeaderLabels(header);tableWidget->setShowGrid(false);tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//选中行为为一行tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//单行选中tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);tableWidget->setFocusPolicy(Qt::NoFocus);//不设置鼠标焦点,不会出现某一行高亮的同时,鼠标所在单元格别选中高亮的情况hLayout[0] = new QHBoxLayout();hLayout[0]->addWidget(fontBox);hLayout[0]->addWidget(fontSizeBox);hLayout[0]->addWidget(underlineBn);hLayout[0]->addWidget(boldBn);hLayout[0]->addWidget(tiltBn);hLayout[0]->addWidget(colorBn);hLayout[0]->addWidget(saveBn);hLayout[0]->addWidget(clearBn);hLayout[1] = new QHBoxLayout();hLayout[1]->addWidget(sendBn);
//    hLayout[1]->addSpacerItem(space1);hLayout[1]->addWidget(onlineNum);
//    hLayout[1]->addSpacerItem(space2);hLayout[1]->addWidget(exitBn);vLayout = new QVBoxLayout();vLayout->addWidget(msgBrowser);vLayout->addLayout(hLayout[0]);vLayout->addWidget(msgTextEdit);vLayout->addLayout(hLayout[1]);hLayout[2] = new QHBoxLayout();hLayout[2]->addLayout(vLayout);hLayout[2]->addWidget(tableWidget);this->setLayout(hLayout[2]);}myWindow::~myWindow()
{}void myWindow::paintEvent(QPaintEvent *event)
{Q_UNUSED(event)QPainter painter(this);painter.setPen(QColor(Qt::black));painter.drawRect(10, this->height()-204, this->width()-212 ,38);painter.drawRect(10, 10, this->width()-212, this->height()-65);
}**void myWindow::closeEvent(QCloseEvent *event)
{Q_UNUSED(event)emit closeMyWindow();
}**

总结

当前的项目中涉及了封装控件功能的实现。在封装的控件中涉及绘画事件重载,窗口关闭重载。跨窗口实现信号接收处理。

qt小项目三 代码实现简易的QQ聊天界面的对话框弹出功能相关推荐

  1. qt小项目 代码实现简易的QQ聊天界面

    qt小项目 代码实现简易的QQ聊天界面 代码 效果图 总结 代码 myDialog.h #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QW ...

  2. qt小项目 代码实现简易的QQ聊天 对话框的界面实现

    效果显示 代码实现 myWindow.h #ifndef WINDOW_H #define WINDOW_H#include <QWidget> #include <QVBoxLay ...

  3. QT小项目--头脑风暴

    QT小项目--头脑风暴 项目演示: 客户端: 单人联网模式 双人匹配对战模式 单机本地练习模式 服务器 服务器主要功能 项目演示: 客户端: 登陆界面,有登陆功能和注册功能,当选择登陆时,如果无此账号 ...

  4. android 仿qq修改头像,Qt:小项目仿QQ修改头像界面,技术点记录

    最近写了一个修改头像功能的UI,布局参考了QQ目前的修改头像界面.如下图 这里主要说明一下两个地方的技术:1.头像图片上层的遮罩层,圆形外部为灰色,内部为全透明:2.上传图片宽高比例可以通过鼠标拖拽移 ...

  5. 51小项目——使用proteus搭建简易的光照度计-(2)

    此篇博客承接上一篇:51小项目--使用proteus搭建简易的光照度计-(1) AD信号处理模块 主要内容为51单片机最小系统的构建 硬件 主控 复位按键 晶振电路 串口下载模块 软件 原理叙述 单片 ...

  6. SAP QM 事务代码QE01录入结果后回车,为啥不弹出Manual Valuation窗口?

    SAP QM 事务代码QE01录入结果后回车,为啥不弹出Manual Valuation窗口? 执行事务代码QE01为检验批#10000000476录入结果, 录入检验结果后回车,没有弹出类似如下图的 ...

  7. OpenCV实践小项目(三) - 停车场车位实时检测

    1. 写在前面 今天整理OpenCV入门的第三个实战小项目,前面的两篇文章整理了信用卡数字识别以及文档OCR扫描, 大部分用到的是OpenCV里面的基础图像预处理技术,比如轮廓检测,边缘检测,形态学操 ...

  8. java小项目 ATM机(简易版)

    目录 说明 项目介绍 功能介绍 结构图 各模块代码 登录界面 服务选择界面 存取款界面 修改密码模块 转账模块 总代码 说明 简单做了一个java的ATM机项目.本项目只能作为一个简单的运行程序.因为 ...

  9. QT小项目Demo【附功能图及源码】

    由于正式项目未开始,之前自个做的小项目,界面全靠想象,现在列出来供参考下 注:资源链接在文末 老老老规矩,正文开始,十步走 后面的同学头抬起来看黑板,看不见就站起来~ 一.登录[可注册,校验用户名密码 ...

最新文章

  1. 两个vlan之间如何通信_如何桥接两个不同的VLAN?
  2. 【蜕变之路】第15天 Eclipse常用问题 (2019年3月5日)
  3. TCP/IP常用端口说明
  4. java递归方法分析
  5. 当神经网络遇上量子计算:谷歌证明量子神经网络可训练图像分类
  6. 一种类型安全的Java HTTP客户端库Retrofit
  7. 在MATLAB中绘制水平线和垂直线的一些方法
  8. graphpad细胞增殖曲线_Graphpad 作图教程 | 这份超详细的生存曲线绘制指南,科研新手一看就会!...
  9. 蓝鸽集团云计算机,‎App Store 上的“蓝鸽教育云”
  10. 六轴传感器+卡尔曼滤波+一阶低通滤波
  11. java 图片深度_图像的色彩值称为图像的颜色深度,黑白图像的颜色深度为( )位。...
  12. ecshop 添加会员头像功能
  13. 华为手机怎么使用读卡器_华为手机怎么绑定门禁卡
  14. 亿发软件:智能中医药信息管理系统,加速智慧药房信息化建设
  15. HOOPS 3DGS技术概述
  16. fhq treap入门
  17. 无法安装此计算机不存在英特尔,win10系统提示无法安装驱动程序,此计算机中没有Intel适配器怎么办...
  18. 四十九、HBase介绍
  19. consistent equation
  20. 为啥黑客都人手一台笔记本电脑?

热门文章

  1. 江苏省农村信用社联合社同城灾备方案及同城灾备网络系统招标公告
  2. WebRTC GCC代码深度解读(12) Probe:ProbeController
  3. Day8(NLB服务搭建)
  4. Docker设置阿里镜像加速器仍然下载很慢
  5. python爬取图片怎么指定位置_python爬取图片
  6. 【题解】【PAT甲】1143 Lowest Common Ancestor (30 分)(树)(BST)(最近公共祖先)
  7. 安兔兔html5评测页面,安兔兔评测(com.antutu.ABenchMark) - 9.0.9-OB - 应用 - 酷安
  8. 【NOIP2013提高组】积木大赛
  9. 什么软件测试三星手机真假好,华为三星苹果到底谁的质量最好 看了这几款的暴力测试后我有了结果...
  10. 硬盘坏了数据可以恢复吗?恢复妙招在这里!