这是一个小程序
test.h
-------------------------
#ifndef TEST_H
#define TEST_H
#include <QPushButton>
#include <QLabel>
#include <QDebug>
class ImageBtn:public QPushButton
{
Q_OBJECT
public:
ImageBtn(QString caption="", QWidget* parent=0):QPushButton(caption, parent){}
protected:
void enterEvent(QEvent *){emit mouseHover(text());}
void focusInEvent(QFocusEvent *){emit mouseHover(text());}
signals:
void mouseHover(QString caption);
};
class ImageLabel:public QLabel
{
Q_OBJECT
public:
ImageLabel(QString text="", QWidget *parent=0, Qt::WindowFlags f=0):QLabel(text, parent,f){}
public slots:
void changeImage(QString imageName)
{
qDebug()<<"image changed to: "<<imageName;
QPixmap pic(imageName);
this->setPixmap(pic);
update();
}
};
#endif
*****************************
test.cpp
-------------------------------
#include <QtGui>
#include "test.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout;
ImageBtn *b1=new ImageBtn("1.png");
ImageBtn *b2=new ImageBtn("2.png");
ImageBtn *b3=new ImageBtn("3.png");
ImageBtn *b4=new ImageBtn("4.png");
layout->addWidget(b1);
layout->addWidget(b2);
layout->addWidget(b3);
layout->addWidget(b4);
ImageLabel *imagelabel = new ImageLabel;
imagelabel->setPixmap(QPixmap(QString::fromUtf8(":/1.png")));
layout->addWidget(imagelabel);
QApplication::connect(b1, SIGNAL(mouseHover(QString)),imagelabel,SLOT(changeImage(QString)));
QApplication::connect(b2, SIGNAL(mouseHover(QString)),imagelabel,SLOT(changeImage(QString)));
QApplication::connect(b3, SIGNAL(mouseHover(QString)),imagelabel,SLOT(changeImage(QString)));
QApplication::connect(b4, SIGNAL(mouseHover(QString)),imagelabel,SLOT(changeImage(QString)));
widget->setLayout(layout);
widget->show();
return app.exec();
}
-----------------------------------------------------------------------------
-----------------------------------------------
这个程序的按键名必须和图片名相同才可以。我想让他们不相同
很多前辈提示我用QMap<QString, QString>来做, 我用了很多次来修改也没改对...
我对我自己彻底失望了...到此来请求各位大神的帮忙!
十分感谢!