#ifndef _MyLineEdit_h_#define _MyLineEdit_h_#include <QtGui/QtGui>#include <QtCore/QtCore>#include "MyPixmapItem.h"//class MyPixmapItem;class MyLineEdit : public QLineEdit{ Q_OBJECTpublic: MyLineEdit(QWidget*pwgt,MyPixmapItem*myp); public slots: //void connector_qstring(); protected: virtual void keyPressEvent(QKeyEvent*pe); virtual void keyReleaseEvent(QKeyEvent*re);private: MyPixmapItem*mypix;};#endif //_MyLineEdit_h_/////////////////////////////////////////////////////////////////////////////////////////#include "MyLineEdit.h"MyLineEdit::MyLineEdit(QWidget*pwgt,MyPixmapItem*myp): QLineEdit(pwgt),mypix(myp){//connect(this,SIGNAL(returnPressed()),this,SLOT(connector_qstring()));}//void MyLineEdit::connector_qstring()//{connect(this, SIGNAL(textChanged(const QString&)),// mypix, SLOT(change_picture(const QString&)));//connect(mypix, SIGNAL(clear_lineedit()),// this, SLOT(clear()));//}void MyLineEdit::keyPressEvent(QKeyEvent*pe){if (pe->key() == Qt::Key_V){connect(this,SIGNAL(textChanged(const QString&)), mypix, SLOT(change_picture(const QString&)));connect(mypix, SIGNAL(clear_lineedit()), this, SLOT(clear()));} QLineEdit::keyPressEvent(pe);}void MyLineEdit:: keyReleaseEvent(QKeyEvent*re){if (re->key() == Qt::Key_V){disconnect(this, SIGNAL(textChanged(const QString&)), mypix, SLOT(change_picture(const QString&)));disconnect(mypix, SIGNAL(clear_lineedit()), this, SLOT(clear()));} QLineEdit::keyReleaseEvent(re);}///////////////////////////////////////////////////////////////////////////////////#include <QtGui/QtGui>#include <QtCore/QtCore>//#include "MyLineEdit.h"class MyPixmapItem : public QObject, public QGraphicsPixmapItem{ Q_OBJECT public: MyPixmapItem(QPixmap& pix,QObject*parent,QGraphicsItem*par); void temp(bool&typ); public slots: void change_picture(const QString& npic); signals: void clear_lineedit(); private: QPixmap pixmap; QString name; bool typetemp;};#endif //_MyPixmapItem_h_/////////////////////////////////////////////////////////////#include "MyPixmapItem.h"MyPixmapItem::MyPixmapItem(QPixmap& pix,QObject*parent,QGraphicsItem*par):QObject(parent),QGraphicsPixmapItem(pix,par){}void MyPixmapItem::change_picture(const QString&npic){ QString puth="images/"; QString p_type=".jpg"; QString f_name; QString n_name; //setText(npic); n_name=npic; f_name=puth+n_name+p_type; //////////// QPixmap temppix; if(temppix.load(f_name)) {setPixmap(temppix);} else {emit clear_lineedit();} //setPixmap(QPixmap(f_name));}void MyPixmapItem:: temp(bool&typ){typ=isWidgetType();}
#include <QtGui/QtGui>#include <QtCore/QtCore>#include "MyLineEdit.h"#pragma comment(lib, "QtGui4.lib")#pragma comment(lib, "QtCore4.lib")// ----------------------------------------------------------------------int main(int argc, char** argv){ QApplication app(argc, argv); QWidget wgt; QLabel* plblAge = new QLabel("Age"); MyLineEdit*txtp =new MyLineEdit(&wgt,plblAge); QVBoxLayout* pvbxLayout = new QVBoxLayout; pvbxLayout->addWidget(plblAge); pvbxLayout->addWidget(txtp); wgt.setLayout(pvbxLayout); wgt.show(); return app.exec();}/////////////////////////////////////////////////////////////////////////////////////////////////#ifndef _MyLineEdit_h_#define _MyLineEdit_h_#include <QtGui/QtGui>#include <QtCore/QtCore>class MyLineEdit : public QLineEdit{public: MyLineEdit(QWidget*pwgt,QLabel*lab); void set_startconnect(int&st);protected: virtual void keyPressEvent(QKeyEvent*pe);private: QLabel*label;};#endif //_MyLineEdit_h_///////////////////////////////////////////////////////////////////////////////#include "MyLineEdit.h"MyLineEdit::MyLineEdit(QWidget*pwgt,QLabel*lab): QLineEdit(pwgt),label(lab){}void MyLineEdit::keyPressEvent(QKeyEvent*pe){if (pe->key()==Qt::Key_Enter){//QMessageBox::information(0,"text",pe->text()); connect(this, SIGNAL(textChanged(const QString&)), label, SLOT(setText(const QString&)));} //pe->ignore(); QLineEdit::keyPressEvent(pe);}