我正在尝试禁用用户先前选择的按钮
void pracownik2::on_pushButton_4_clicked(){
this->setWindowTitle("EKRAN");
QWidget *centralWidget = new QWidget;
int licznik=1;
QString licz;
//QString kolumny = ui->lineEdit->text();
//QString wiersze = ui->lineEdit_2->text();
miejsca2 = ui->lineEdit_3->text().toInt();
//QPushButton *button[wiersze.toInt()][kolumny.toInt()];
QPushButton *button[3][6];
QGridLayout *controlsLayout = new QGridLayout;
for(int i=0;i<3;i++)
{
for(int j=0;j<6;j++)
{
licz = QString::number(licznik);
licznik++;
button[i][j] = new QPushButton(licz);
button[i][j]->setCheckable(1);
if(tab[i][j]==1)
button[i][j]->setEnabled(false);
controlsLayout->addWidget(button[i][j], i, j);
}
}
QPushButton *okej = new QPushButton("Zatwierdź");
QPushButton *anul = new QPushButton("Anuluj");
controlsLayout->addWidget(okej, 3, 0);
controlsLayout->addWidget(anul, 3, 1);
controlsLayout->setHorizontalSpacing(0);
controlsLayout->setVerticalSpacing(0);
centralWidget->setLayout(controlsLayout);
setCentralWidget(centralWidget);
for(int i=0;i<3;i++)
{
for(int j=0;j<6;j++)
{
connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool)));
}
}
connect(anul,SIGNAL(clicked()),this,SLOT(close()));
connect(okej,SIGNAL(clicked()),this,SLOT(okay2()));}
void pracownik2::tescik(bool t){
if (t)
{
tab[i][j]=1;
miejsca++;
}
else
{
tab[i][j]=0;
miejsca--;
}}
但我的&#39; tescik&#39;功能并不知道&#39; i&#39;和&#39; j&#39;是的,项目不会编译,我的问题是如何使数组中的选中按钮设置值1并取消选中将其恢复为0.我想我必须编辑&#39; connect&#39;但我不知道如何
@EDIT 我正在尝试制作这一行
connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool,int i,int j)));
传递&#39;我&#39;和&#39; j&#39;当前按钮的功能,但它没有工作
答案 0 :(得分:0)
您无法连接
connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool,int i,int j)));
因为toggled
如何知道i,j
的哪些值传递给tescik
?
然而,你可以在QPushButtons上编写一个包装器,这样你就可以使用QSignalMapper将它们的toggled(bool)
信号连接到这个包装器的单个插槽toggled( int, int)
。然后你可以在这样的包装器中管理所有这些逻辑:
class ButtonHelperEntry
{
public:
int i_;
int j_;
QPushButton* button_;
}
void ButtonHelper::initialize( const std::vector<
QSharedPointer<ButtonHelperEntry> >& entries)
{
entries_ = entries;
mapper_.reset(new QSignalMapper(this));
EntriesIterator it = entries_.begin();
while ( it!=entries_.end())
{
connect(( *it)->button_, SIGNAL( toggled(bool)), mapper_.data(),
SLOT(map()));
mapper_->setMapping( ( *it)->button_, (int)( *it)->i_, (int)( *it)->j_);
it++;
i++;
}
connect( mapper_.data(), SIGNAL( mapped(int,int)),
this, SIGNAL( toggled(int,int)));
connect( this, SIGNAL( toggled(int)), this, SLOT( updateValue(int)));
}
答案 1 :(得分:0)
我会将i和j设置为每个按钮的属性:
...
button[i][j]->setProperty("i", i);
button[i][j]->setProperty("j", j);
...
然后在tescik()中获取发件人并加载i和j:
void pracownik2::tescik(bool t) {
QObject * pSender = sender();
int i = pSender->property("i").toInt();
int j = pSender->property("j").toInt();
if (t) {
...