【问题标题】:How can I connect QButtton array?如何连接 QButton 数组?
【发布时间】:2017-05-05 17:15:29
【问题描述】:

我是韩国人,所以请理解我不擅长英语。

我只是用 Qt5 做 POS 系统。

当我按下某个按钮(红色)时,我想制作,显示在桌子上(黄色);

像这样:

enter image description here

所以,我读取了一些具有某些项目名称的文件并将它们放入 QPushButton 数组中。

然后我像这样连接 QPushButton 数组:

void qt_test::put_item(QWidget *Widget, QString location){
QGridLayout *layout = new QGridLayout();
QPushButton *button[25]; //set QPushButton array;
QString name[25];  //store name of item;

fileio file;
file.file_io2(name, location);  //read file and get name of item;

int temp=0;
for(int i=0;i<5;i++){
    for(int j=1;j<6;j++,temp++){
       //insert name in QPushButton
        button[temp] = new QPushButton(name[temp]);  
        button[temp]->setMinimumSize(10,70);
        layout->addWidget(button[temp],i,j);
    }
}

Widget->setLayout(layout);

//connect QPushArray.
connect(*button,SIGNAL(clicked()),this,SLOT(input_item()));

}

这是我认为的插槽

void qt_test::input_item(){
    if(!(P_ROW==9)){
        ui.sel_item_table->item(P_ROW,0)->setText("test");
        ++P_ROW;
    }

问题是,

  1. 我不知道如何将 QPushBotton 中的文本放在 QTable 中。

  2. 如何控制插槽中连接的按钮。 在我的代码中,只有一个按钮有效……并非所有按钮。 只是洋葱汤;(

【问题讨论】:

    标签: qt5 qtablewidget qpushbutton


    【解决方案1】:

    您需要连接每个单独的按钮:

    for(int i=0;i<5;i++){
        for(int j=1;j<6;j++,temp++){
           //insert name in QPushButton
            button[temp] = new QPushButton(name[temp]);  
            button[temp]->setMinimumSize(10,70);
            layout->addWidget(button[temp],i,j);
    
            connect( button[temp], &QPushButton::clicked, [=] { input_item( button[temp] ); } );   
        }
    }
    
    ...
    
    void qt_test::input_item( QPushButton* button )
    {
        ui.sel_item_table->item(P_ROW,0)->setText( button->text() );
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      相关资源
      最近更新 更多