问题
I am trying to disable button previously chosen by user
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--;
}}
but my 'tescik' function doesn't know what 'i' and 'j' are and the project won't compile, my question is how to make checked button set value 1 in the array and unchecked restore it to 0. I guess I have to edit 'connect' line but I have no idea how
@EDIT I am trying to make this line
connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool,int i,int j)));
pass 'i' and 'j' of current button to function but it doesn't work
回答1:
You can't connect
connect(button[i][j],SIGNAL(toggled(bool)),this,SLOT(tescik(bool,int i,int j)));
because how would toggled
know what values of i,j
pass to tescik
?
You can however write a wrapper over QPushButtons so you can connect their toggled(bool)
signals to single slot toggled( int, int)
of this wrapper using QSignalMapper. Then you can manage all this logic in such wrapper:
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)));
}
回答2:
I would set i and j as properties of each button:
...
button[i][j]->setProperty("i", i);
button[i][j]->setProperty("j", j);
...
Then in tescik() get the sender and load i and j:
void pracownik2::tescik(bool t) {
QObject * pSender = sender();
int i = pSender->property("i").toInt();
int j = pSender->property("j").toInt();
if (t) {
...
来源:https://stackoverflow.com/questions/28125466/qt-disabling-dynamic-buttons