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

实现效果

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

打开一个窗口
qt小项目三 代码实现简易的QQ聊天界面的对话框弹出功能

再次打开该窗口

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

补充后的代码

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_OBJECT

public:
    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_OBJECT

public:
    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);
//    tableWid
    QStringList 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();
}**

总结

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

上一篇:PYQT5(25)-图形和特效-QSS的UI美化


下一篇:Spring 笔记——核心-AOP篇