• 7091阅读
  • 6回复

如何在QT3 中实现按钮风格 为扁平没有那种3D的风格 [复制链接]

上一主题 下一主题
离线darkhucx
 

只看楼主 倒序阅读 楼主  发表于: 2006-05-10
如何在QT3 中实现按钮风格 为扁平没有那种3D的风格
初学QT 不是很懂 谢谢各位的帮助 很急 谢谢!!
[ 此贴被XChinux在2006-05-10 09:28重新编辑 ]
离线carbuncle
只看该作者 1楼 发表于: 2006-05-11
mpBtn->setFlat( true );
离线darkhucx

只看该作者 2楼 发表于: 2006-05-13
楼上的兄弟 这样做我试了虽然是扁平的风格,但是点击按钮后还是会显示出它的边框啊
我现在考虑用label做 但是焦点没有映射在label里 而在label外的窗体 有高手能帮忙给看看吗
#ifndef LAB_H
#define LAB_H

#include <qwidget.h>
#include <qlabel.h>
#include <qevent.h>
#include <qpixmap.h>

class Lab : public QLabel
{
    Q_OBJECT
  public:
  Lab( QWidget *parent=0, const char *name=0 );
  void mouseState(bool, QMouseEvent *);
  public slots:
      void PixChange(bool);
  signals:
  void catchMouse(bool);
  protected:
  void mousePressEvent(QMouseEvent *);
  void mouseReleaseEvent(QMouseEvent *);
  private:
      bool mouseSat;
      QPixmap pix1;
      QPixmap pix2;
};

#endif
#include <qapplication.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qevent.h>

#include "lab.h"

Lab::Lab(QWidget *parent, const char *name)
  :QLabel(parent, name)
{
  QLabel *aa = new QLabel("hello", this);
  aa->setGeometry(100,100,200,100);
  QPixmap pix1("STOP_BTN.png");
  QPixmap pix2("PLAY_BTN.png");
  mouseSat = false;
 
}

void Lab::mousePressEvent(QMouseEvent *e)
{  
        if ( rect().contains( e->pos() ) )
          emit catchMouse(true);
        //QLabel *c = new QLabel("aaa",this);
        //QPixmap pim("STOP_BTN.png");
        //c->setPixmap(pim);
        //c->setAlignment(AlignCenter);
    mouseSat = true;
    mouseState(mouse, e);  
}

void Lab::mouseReleaseEvent( QMouseEvent *e )
{  
      mouseSat = false;
      mouseState(mouseSat, e);
}
void Lab::mouseState(bool mouseSat,QMouseEvent *e)
{  
      bool mousta = mouseSat;
     if(mousta == true)
     {
             emit catchMouse(true);
     }
     else if(mouseSat == false)
     {
        emit catchMouse(false);        
     }
}

void Lab::PixChange(bool mouseSat)
{
      QLabel *bb = new QLabel("bb", 0);
  if(mouseSat == true)
  {
           bb->setPixmap(pix1);
           bb->show();
  }    
  else if(mouseSat == false)
  {
    bb->setPixmap(pix2);
    bb->show();    
  }
}

int main( int argc, char **argv )
{
  QApplication a( argc, argv );
  QPixmap pi("PLAY_BTN.png");
  QPixmap pi2("STOP_BTN.png");
  Lab aa;
  QObject::connect(&aa, SIGNAL(catchMouse(mouseSat)), &a, SLOT(quit()));
  a.setMainWidget(&aa);
  aa.show();
  return a.exec();
}
离线darkhucx

只看该作者 3楼 发表于: 2006-05-13
啊 发错了 我改一下
离线darkhucx

只看该作者 4楼 发表于: 2006-05-13
#ifndef LAB_H
#define LAB_H

#include <qwidget.h>
#include <qlabel.h>
#include <qevent.h>
#include <qpixmap.h>

class Lab : public QLabel
{
    Q_OBJECT
  public:
  Lab( QLabel *parent=0, const char *name=0 );
  public slots:
      void PressChange(QMouseEvent *);
      void ReleaseChange(QMouseEvent *);
  signals:
  void clicked();
  protected:
  void mousePressEvent(QMouseEvent *);
  void mouseReleaseEvent(QMouseEvent *);
  private:
  QLabel *la;
  bool state;
  QPixmap pix1;
  QPixmap pix2;
};

#endif

#include <qapplication.h>
#include <qlabel.h>
#include <qpixmap.h>
#include <qevent.h>

#include "lab.h"

Lab::Lab(QLabel *parent, const char *name)
  :QLabel(parent, name)
{
   
    QPixmap pix3("STOP_BTN.png");
  la = new QLabel("hello", this);
  //la->setIcon(pix3);
  la->setBackgroundColor(QColor(100,100,100));
  la->setGeometry(0,0,100,100);
  //pix1 = pix3;
  //QPixmap pix4("PLAY_BTN.png");
  //pix2 = pix4;
  //state = false;
}

void Lab::mousePressEvent(QMouseEvent *e)
{  
      PressChange(e);
      emit clicked();
}

void Lab::mouseReleaseEvent(QMouseEvent *e )
{  
      ReleaseChange(e);
      emit clicked();
}

void Lab::PressChange(QMouseEvent *e)
{  
    QPixmap pix1("PLAY_BTN.png");
    QPixmap pix2("STOP_BTN.png");
    switch(e->button())
  {
    case Qt::LeftButton:
         la->setText("OK!!");
        la->setGeometry(0,0,100,100);
        //la->show();
        break;
    case Qt::RightButton:
    case Qt::MidButton:
    case Qt::NoButton:
    case Qt::ShiftButton:
    case Qt::ControlButton:
    case Qt::AltButton:
    case Qt::Keypad:
    case Qt::KeyButtonMask:
    case Qt::MouseButtonMask:
         break;            
    }
   
}

void Lab::ReleaseChange(QMouseEvent *e)
{  
    QPixmap pix1("PLAY_BTN.png");
    QPixmap pix2("STOP_BTN.png");
    switch(e->button())
  {
    case Qt::LeftButton:
         //la->setIcon(pix2);
         la->setText("Hello!");
        la->setGeometry(0,0,100,100);
        //la->show();
        break;
    case Qt::RightButton:
    case Qt::MidButton:
    case Qt::NoButton:
    case Qt::ShiftButton:
    case Qt::ControlButton:
    case Qt::AltButton:
    case Qt::Keypad:
    case Qt::KeyButtonMask:
    case Qt::MouseButtonMask:        
         break;    
    }
   
}

int main( int argc, char **argv )
{
  QApplication a( argc, argv );
  //QPixmap pi("PLAY_BTN.png");
  //QPixmap pi2("STOP_BTN.png");
  Lab aa;
  QObject::connect(&aa, SIGNAL(clicked()), &aa, SLOT(PixChange(QMouseEvent *)));
  QObject::connect(&aa, SIGNAL(clicked()), &aa, SLOT(ReleaseChange(QMouseEvent *)));    
  a.setMainWidget(&aa);
  aa.show();
  return a.exec();
}
离线sflute
只看该作者 5楼 发表于: 2006-05-18
你可以自己写一个类,继承QLabel类,然后在这个类里重写mousePressEvent(QMouseEvent * e)这个函数,下面是我的程序里重写mousePressEvent(QMouseEvent * e)的一段,在单击label时弹出一个对话框。

void PixLabel::mousePressEvent(QMouseEvent * e){
   QDialog *dlg = new QDialog( this, "dlg", true );
   dlg->show();

   QLabel::mousePressEvent(e);
}
离线darkhucx

只看该作者 6楼 发表于: 2006-05-18
谢谢楼上的 我也解决了 是继承关系那搞错了
快速回复
限100 字节
 
上一个 下一个