• 4629阅读
  • 4回复

【求助】如何用QMap? [复制链接]

上一主题 下一主题
离线omelas
 

只看楼主 正序阅读 楼主  发表于: 2009-06-24
这是一个小程序

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>来做, 我用了很多次来修改也没改对...
我对我自己彻底失望了...到此来请求各位大神的帮忙!
十分感谢!
离线omelas

只看该作者 4楼 发表于: 2009-06-26
您能具体点吗
离线bienew

只看该作者 3楼 发表于: 2009-06-25
按键名和文件名相同,是由于你的程序限制啊
你emit mouseHover(text),这个text取不一样就行了
并且,你可以把Widget做成一个类
在里头做一个slot,接收所有的mouseHover(text),不就行了?
谁的text或者谁的id,对应的image change就是
离线omelas

只看该作者 2楼 发表于: 2009-06-25
contirnue求解答啊!!!
离线omelas

只看该作者 1楼 发表于: 2009-06-24
求解答啊!!!
快速回复
限100 字节
 
上一个 下一个