首页 > 其他分享 >qt 按键数组

qt 按键数组

时间:2023-01-17 11:11:25浏览次数:58  
标签:https qt button controller connect 数组 按键 net APP

按键配置

    QPushButton * controller_button_[APP_NUM_OF_CONTROLLER];

    for (int i = 0; i < APP_NUM_OF_CONTROLLER; i++) {
        controller_button_[i] = new QPushButton(this);
    }

    int col_size = (width - APP_COL_SPACE * 1) / APP_NUM_OF_CONTROLLER_ROW;
    int row_size = (height - APP_ROW_SPACE * 2) / (APP_NUM_OF_CONTROLLER / APP_NUM_OF_CONTROLLER_ROW + 1 + 2);
    qDebug() << "col size: " << col_size << ", row size = " << row_size;

    for (int i = 0; i < APP_NUM_OF_CONTROLLER; i++) {
        //controller_button_[i] = new QPushButton(this);
        int row = i / APP_NUM_OF_CONTROLLER_ROW;
        int col = i % APP_NUM_OF_CONTROLLER_ROW;

        controller_button_[i]->move(col * col_size, row * row_size);
        controller_button_[i]->resize(col_size - APP_COL_SPACE, row_size - APP_ROW_SPACE);
        controller_button_[i]->setText(QString::number(i + 1));
    }

按钮处理

使用 qt5 的 connect 写法,编译有问题,使用 qt4 的 connect 写法就没有问题了。 q4 使用 QSignalMapper 的方法,qt5 可以使用 lambda 来实现. 类似 connect(btn, &QPushButton::clicked, this, [=](){func(i);});

    signalMapper_ = new QSignalMapper(this);

    for (int i = 0; i < APP_NUM_OF_CONTROLLER; i++) {
        //connect(controller_button_[i], &QPushButton::clicked, this, &MainWindow::ClickDetail);
        //connect(controller_button_[i], &QPushButton::clicked, signalMapper_, &QSignalMapper::map);
        connect(controller_button_[i], SIGNAL(clicked()), signalMapper_, SLOT(map()));
        signalMapper_->setMapping(controller_button_[i], i);
    }
    //connect(signalMapper_, &QSignalMapper::mapped, this, &MainWindow::ClickDetail);
    connect(signalMapper_, SIGNAL(mapped(int)), this, SLOT(ClickDetail(int)));

参考:

Qt使用数组实现控件批处理
https://blog.csdn.net/nchu_zhangyiqing/article/details/111871693

QT在ui中如何把几个按钮设置成数组的 ?
https://bbs.csdn.net/topics/390024181

Qt 中QButtonGroup 的用法
https://blog.csdn.net/tax10240809163com/article/details/92708983

Qt之QButtonGroup简单使用
https://blog.csdn.net/u013782830/article/details/53392072

Qt实现动态(个数不确定)的一组按钮
https://blog.csdn.net/qq_37441588/article/details/118681941

Qt简单的按钮槽函数传递参数方法
https://blog.csdn.net/nchu_zhangyiqing/article/details/111871484

Qt使用connect函数时向slot传递参数
https://blog.csdn.net/imred/article/details/72940365

QSignalMapper的使用和使用场景
https://www.cnblogs.com/tudou/p/11586454.html

标签:https,qt,button,controller,connect,数组,按键,net,APP
From: https://www.cnblogs.com/ramlife/p/17057301.html

相关文章