这是使用Qt 4在按下并释放QPushButton
时更改图像位图的代码:
void MainWindow::on_pushButton_2_pressed()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("images (*.bmp)"));
if(QString::compare(filename, QString()) != 0)
{
QImage image;
bool volid = image.load(filename);
if(volid)
{
ui->lineEdit->setText(filename);
QPixmap pixmap (filename);
QIcon ButtonIcon (pixmap);
ui->pushButton->setIcon(ButtonIcon);
ui->pushButton->setAutoRepeat(true);
}
else
{
// Error handling
}
}
}
void MainWindow::on_pushButton_3_released()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Choose"), "", tr("images (*.bmp)"));
if(QString::compare(filename, QString()) != 0)
{
QImage image;
bool volid = image.load(filename);
if(volid)
{
ui->lineEdit_2->setText(filename);
QPixmap pixmap (filename);
QIcon ButtonIcon (pixmap);
ui->pushButton->setIcon(ButtonIcon);
ui->pushButton->setAutoRepeat(false);
}
else
{
// Error handling
}
}
}
下图显示我需要选择两张图片并添加按钮操作,以便在按下和释放时更改。