0
我是新來的qt。其實我有我的項目形象。我需要通過單擊事件來顯示圖像以及圖像上方的文字。我試過使用QLabel
來顯示圖片和文字,但不支持點擊事件。以下是我已經試過:如何在QT中使用點擊事件的圖片文字
mylabel->setText("Wh");
mylabel->setStyleSheet(QString("QLabel {background-image: url(\"sample.bmp\");color : blue;}"));
mylabel->setGeometry(10,10,78,78);
mylabel->setFont(QFont("Arial", 12));
mylabel->setAlignment(Qt::AlignCenter);
我試過鼠標事件,它也能正常工作,但我不知道如何比較與標籤位置(XPOS,ypos)的事件位置。另外,如果我在運行時更改標籤的位置(setGeometry),則比較標籤位置(xpos,ypos)更加困難。我用我的mouseRelease事件一樣,
void SampleProject::mouseReleaseEvent(QMouseEvent* event)
{
int Xpos=event->x();
int Ypos=event->y();
QString s = QString::number(Xpos);
QString t = QString::number(Ypos);
QMessageBox::question(this, s, t, QMessageBox::Yes|QMessageBox::No);
emit clicked(event->pos());
}
待辦事項你真的需要一個QLabel?如果你正在尋找一個可點擊的圖像;你可以設計類似於QLabel的QPushButton:'ui-> pushButton-> setStyleSheet(QString(「QPushButton {background-image:url(\」img.jpg \「); border-style:none}」));'' – HeyYO