詳解兩個(gè)自制Qt Widget應(yīng)用案例實(shí)現(xiàn)
Qt Widget應(yīng)用案例實(shí)現(xiàn)是本文要介紹的內(nèi)容,文章中主要講解了自制的兩個(gè)Qt Widget應(yīng)用,具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。Qt自帶的Widget不大夠用了,于是和他一起做了兩個(gè)。一個(gè)是圖片按鈕。這是一個(gè)基于QAbstractButton的按鈕,按鈕的實(shí)體是圖片,coyotte508實(shí)現(xiàn)的時(shí)候忽略了按鈕被按下時(shí)的效果,我給他完善了一下。
- class QImageButtonP : public QAbstractButton
- {
- Q_OBJECT
- public:
- QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked ="");
- QSize sizeHint() const;
- QSize minimumSizeHint() const;
- QSize maximumSize() const;
- void changePics(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked = "");
- protected:
- void paintEvent(QPaintEvent *e);
- void mouseMoveEvent(QMouseEvent *e);
- void mousePressEvent(QMouseEvent *e);
- void mouseReleaseEvent(QMouseEvent *e);
- private:
- QPixmap myPic, myHoveredPic, myCheckedPic, myPressedPic;
- int lastUnderMouse; // last mouse pos recorded
- bool bpressed;
- enum State {
- Normal,
- Hovered,
- Checked,
- Pressed
- };
- int lastState;
- };
- QImageButtonP::QImageButtonP(const QString &normal, const QString &hovered, const QString &pressed, const QString &checked)
- : myPic(normal), myHoveredPic(hovered), myPressedPic(pressed), lastUnderMouse(-1), bpressed(false)
- {
- setFixedSize(myPic.size());
- #if defined(WIN32) || defined(WIN64)
- setMask(::mask(myPic));
- #endif
- lastState = Normal;
- /* Both are necessary for some styles */
- setMouseTracking(true);
- setAttribute(Qt::WA_Hover, true);
- if (checked != "")
- myCheckedPic = QPixmap(checked);
- }
- void QImageButtonP::changePics(const QString &normal, const QString &hovered,const QString &pressed, const QString &checked)
- {
- myPic = QPixmap(normal);
- myHoveredPic = QPixmap(hovered);
- myPressedPic = QPixmap(pressed);
- if (checked != "")
- myCheckedPic = QPixmap(checked);
- #if defined(WIN32) || defined(WIN64)
- setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) :
- (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));
- #endif
- update();
- }
- void QImageButtonP::mousePressEvent(QMouseEvent *e)
- {
- bpressed = true;
- QAbstractButton::mousePressEvent(e);
- }
- void QImageButtonP::mouseReleaseEvent(QMouseEvent *e)
- {
- bpressed = false;
- QAbstractButton::mouseReleaseEvent(e);
- }
- QSize QImageButtonP::sizeHint() const
- {
- return myPic.size();
- }
- QSize QImageButtonP::minimumSizeHint() const
- {
- return sizeHint();
- }
- QSize QImageButtonP::maximumSize() const
- {
- return sizeHint();
- }
- void QImageButtonP::paintEvent(QPaintEvent *e)
- {
- QPainter painter(this);
- int newState;
- if ((this->isChecked()) && !myCheckedPic.isNull()) {
- newState = Checked;
- painter.drawPixmap(e->rect(), myCheckedPic, e->rect());
- }else if(this->isDown () && !myPressedPic.isNull()) {
- newState = Pressed;
- painter.drawPixmap(e->rect(), myPressedPic, e->rect());
- }else {
- if (!underMouse()) {
- newState = Normal;
- painter.drawPixmap(e->rect(), myPic, e->rect());
- }
- else {
- newState = Hovered;
- painter.drawPixmap(e->rect(), myHoveredPic, e->rect());
- }
- }
- if (newState != lastState) {
- lastState = newState;
- #if defined(WIN32) || defined(WIN64)
- setMask(lastState == Checked ? ::mask(myCheckedPic) : (lastState == Normal ? ::mask(myPic) :
- (lastState == Pressed ? ::mask(myPressedPic) : ::mask(myHoveredPic))));
- #endif
- }
- lastUnderMouse = underMouse();
- }
- void QImageButtonP::mouseMoveEvent(QMouseEvent *)
- {
- if (int(underMouse()) == lastUnderMouse)
- return;
- update();
- }
另一個(gè)是IRC聊天輸入框模式的一個(gè)輸入框,輸入框?qū)鈽?biāo)-上和光標(biāo)-下兩個(gè)鍵盤(pán)輸入產(chǎn)生反應(yīng),能夠回到之前輸入的內(nèi)容或者返回當(dāng)前內(nèi)容。
- class QIRCLineEdit : public QLineEdit
- {
- Q_OBJECT
- public:
- QIRCLineEdit();
- private:
- void keyPressEvent(QKeyEvent *);
- QList<QString> m_Inputlist1;//Stores the inputed strings,up list.
- QList<QString> m_Inputlist2;//Stores the inputed strings,down list.
- //QString m_Currentline;//Stores a copy of the current text in the LineEdit.
- public slots:
- void myTextEdited();
- void myclear();
- };
- QIRCLineEdit::QIRCLineEdit()
- {
- connect(this,SIGNAL(textEdited(QString)),this,SLOT(myTextEdited()));
- }
- void QIRCLineEdit::keyPressEvent(QKeyEvent *e)
- {
- if(e->key() == Qt::Key_Up) {
- if(text()==""){
- if(m_Inputlist1.empty()){
- }else{
- setText(m_Inputlist1.last());
- m_Inputlist1.removeLast();
- }
- }else if(text()!=""){
- m_Inputlist2.append(text());
- if(m_Inputlist1.empty()){
- clear();
- }else{
- setText(m_Inputlist1.last());
- m_Inputlist1.removeLast();
- }
- }
- }else if(e->key() == Qt::Key_Down) {
- if(text()==""){
- if(m_Inputlist2.empty()){
- }else{
- setText(m_Inputlist2.first());
- m_Inputlist2.removeFirst();
- }
- }else if(text()!=""){
- if(m_Inputlist2.empty()){
- m_Inputlist1.append(text());
- clear();
- }else{
- m_Inputlist1.append(text());
- setText(m_Inputlist2.first());
- m_Inputlist2.removeFirst();
- }
- }
- }else{
- QLineEdit::keyPressEvent(e);
- }
- }
- void QIRCLineEdit::myTextEdited()
- {
- //if(!m_Inputlist2.empty()) m_Inputlist1.append(m_Currentline);
- m_Inputlist2.clear();
- }
- void QIRCLineEdit::myclear()
- {
- if(text()!=""){
- m_Inputlist1.append(text());
- }
- QLineEdit::clear();
- }
小結(jié):詳解兩個(gè)自制Qt Widget應(yīng)用案例實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過(guò)Qt Widget應(yīng)用內(nèi)容的學(xué)習(xí)能對(duì)你有所幫助!