随笔 - 52  文章 - 0  评论 - 13  阅读 - 16万

QT 信号(槽)绑定的使用_connect

第一种方式:

 connect(ui->rbtnRed,SIGNAL(clicked()),this,SLOT(setTextFontColor()));

说明:

ui->rbtnRed,是界面上的控件,即发出信号的主体;

clicked(),是对应控件的信号(鼠标点击);

this,即当前窗口对象,指槽函数所属对象(处理信号的主体);

setTextFontColor(),是自定义的槽函数,定义如下(Dialog.h):

private slots:
    void setTextFontColor();

槽函数的具体实现(Dialog.cpp):

复制代码
void Dialog::setTextFontColor()
{
    QPalette plet=ui->txtEdit->palette();
    if(ui->rbtnBlue->isChecked())
        plet.setColor(QPalette::Text,Qt::blue);
    else if(ui->rbtnRed->isChecked())
        plet.setColor(QPalette::Text,Qt::red);
    else if(ui->rbtBlack->isChecked())
        plet.setColor(QPalette::Text,Qt::black);

    ui->txtEdit->setPalette(plet);
}
复制代码

 

第二种方式:

复制代码
    connect(ui->rbtnRed,&QRadioButton::clicked,[this]{

        QPalette plet=ui->txtEdit->palette();
        if(ui->rbtnBlue->isChecked())
            plet.setColor(QPalette::Text,Qt::blue);
        else if(ui->rbtnRed->isChecked())
            plet.setColor(QPalette::Text,Qt::red);
        else if(ui->rbtBlack->isChecked())
            plet.setColor(QPalette::Text,Qt::black);

        ui->txtEdit->setPalette(plet);
    });
复制代码

说明:这里将槽函数部分进行简略书写,这样可以不用提前定义一个槽函数,而是直接编写函数实现。

如果信号标签中带参数:

    connect(ui->chkUnderline,&QCheckBox::clicked,[this](bool checked){
        QFont font = ui->txtEdit->font();
        font.setUnderline(checked);

        ui->txtEdit->setFont(font);
    });

如果信号发送主体和接收主体,不再同一个线程中创建,则需要使用(跨)线程同步标记:

    connect(this,   &SystemSync::sendData_UDP,   _SystemUart,  &SystemUart::sendData_UDP,   Qt::QueuedConnection);

 

posted on   云梦鸿  阅读(1231)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示