在QT中设计通讯录并实现跳转发短信

在qt中设计通讯录页面,并为按钮添加图片,当点击短信图标后,跳转到短信编辑页面。目前,只是实现了页面设计,对于具体的操作还未实现。本博客主要记录该过程以及遇到的问题

通讯录页面

主要功能包括:展示所有的联系人,点击相应的图标后跳转到对应的页面,搜索联系人

知识点总结
  1. 设置边框的弧度,例如将TextEdit的边框设置为圆角

首先在ui文件中添加一个TextEdit,然后选中该文本框,右击选择“改变样式表”

可以通过添加颜色来对文字框的背景颜色,边框颜色等进行修改。最后点击确定,得到最终的结果如下

对其他的操作类似,只是将QTextEdit换成相应的需要修改的对象。

参考:

Qt:62---Qt样式表(setStyleSheet函数、.qss文件)_51CTO博客_qt样式表

关于Qt中setStyleSheet()易踩的坑——样式覆盖_gkzscs的博客-CSDN博客_qt setstylesheet

  1. 在TableWidget中添加按钮,并实现跳转

//        创建一个QPushButton控件
        QPushButton *pushButton2 = new QPushButton();

// 为按钮添加图片
        QIcon icon2;
        icon2.addFile(图片所在的路径);
        pushButton2->setIcon(icon2);
        //建立信号槽
        connect(pushButton2, SIGNAL(clicked(bool)), this, SLOT(clickPhone()));
        //将QPushButton控件设置到QTableWidget中
        //将按钮插入到第i行,第j列的单元格中
        ui->tableWidget_2->setCellWidget(i, j ,pushButton2);

clickPhone()为自定义的函数,当用户点击该按钮后,执行该函数。

注意:自定义的函数,在头文件中,必须存放在private slots下才会生效,即

private slots:
    void clickPhone();
    void clickMessage();

参考:

QTableWidget中添加QComboBox/QPushButton控件并响应控件点击_TanChengkai的博客-CSDN博客

代码展示

头文件(tongxun.h)

#ifndef TONGXUN_H
#define TONGXUN_H

#include <QDialog>
#include "message.h"

namespace Ui {
class tongxun;
}

class tongxun : public QDialog
{
    Q_OBJECT

public:
    explicit tongxun(QWidget *parent = nullptr);
    ~tongxun();
    void setTableWidgeData();

private slots:
    void clickPhone();//点击打电话图标
    void clickMessage();//点击发短信图标

private:
    Ui::tongxun *ui;
    message msg;
};

#endif // TONGXUN_H

tongxun.cpp

#include "tongxun.h"
#include "ui_tongxun.h"
#include <QComboBox>
#include <QPushButton>
#include <QDebug>

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

    this->setWindowFlags(this->windowFlags()&Qt::WindowMaximizeButtonHint&Qt::WindowMinimizeButtonHint);//为对话框添加上最大化和最小化按钮

    //表格2
    ui->tableWidget_2->setColumnCount(4);
    //设置行数
    ui->tableWidget_2->setRowCount(15);

    //设置充满表格宽度
    //ui->tableWidget_2->horizontalHeader()->setStretchLastSection(true);
    //设置列的大小(固定每一列的大小)
    ui->tableWidget_2->setColumnWidth(0,45);
    ui->tableWidget_2->setColumnWidth(1,120);
    ui->tableWidget_2->setColumnWidth(2,40);
    ui->tableWidget_2->setColumnWidth(3,40);
    //隐藏列表头和行表头
    ui->tableWidget_2->verticalHeader()->setVisible(false);
    ui->tableWidget_2->horizontalHeader()->setVisible(false);
    //去除水平滚动条
    ui->tableWidget_2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //去除TableWidget中的线
    ui->tableWidget_2->setShowGrid(false);

    setTableWidge2Data();
}

tongxun::~tongxun()
{
    delete ui;
}


//在tableWidget中添加按钮,并为按钮添加图片,当点击短信图标时,跳转到编辑短信页面
void tongxun::setTableWidge2Data()
{
    //设置头像
    for(int i=0;i<15;i++)
    {
        QPushButton *pushButton0 = new QPushButton();
        QIcon icon0;
        icon0.addFile("D:/C++Code/PhoneTongxun/ICON图片/3.jpg");
        pushButton0->setIcon(icon0);
        //将QPushButton控件设置到QTableWidget中
        ui->tableWidget_2->setCellWidget(i, 0 ,pushButton0);
    }

    //设置姓名
    for(int i=0;i<15;i++)
    {
        QLabel *lable1 = new QLabel;
        lable1->setText("zhangsan");
        ui->tableWidget_2->setCellWidget(i,1,lable1);
    }

    //设置打电话
    for(int i=0;i<15;i++)
    {
        //创建一个QPushButton控件
        QPushButton *pushButton1 = new QPushButton();
        QIcon icon1;
        icon1.addFile("D:/C++Code/PhoneTongxun/ICON图片/电话.jpg");
        pushButton1->setIcon(icon1);
        //建立信号槽
        connect(pushButton1, SIGNAL(clicked(bool)), this, SLOT(clickPhone()));
        //将QPushButton控件设置到QTableWidget中
        ui->tableWidget_2->setCellWidget(i, 2 ,pushButton1);
    }

    //设置发短信
    for(int i=0;i<15;i++)
    {
//        创建一个QPushButton控件
        QPushButton *pushButton2 = new QPushButton();

// 为按钮添加图片
        QIcon icon2;
        icon2.addFile("D:/C++Code/PhoneTongxun/ICON图片/短信.jfif");
        pushButton2->setIcon(icon2);
//        //建立信号槽
        connect(pushButton2, SIGNAL(clicked(bool)), this, SLOT(clickPhone()));
//        //将QPushButton控件设置到QTableWidget中
        ui->tableWidget_2->setCellWidget(i, 3 ,pushButton2);
    }
}

void tongxun::clickPhone()
{
    msg.show();
}

void tongxun::clickMessage()
{
    msg.show();
}
短信编辑页面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值