• 6180阅读
  • 2回复

请教:qt怎么自定义一个按钮? [复制链接]

上一主题 下一主题
离线weitaotao110
 

只看楼主 倒序阅读 楼主  发表于: 2009-10-28
rt··请教:qt怎么自定义一个按钮?
My blog: www.hacktao.com
走好每一天!
离线午小夜

只看该作者 1楼 发表于: 2009-10-29
here is an example of using Qt Style Sheet:
  1. Widget::Widget(QWidget *parent)
  2.     : QWidget(parent)
  3. {
  4. //    this->setMouseTracking(true);
  5. //    qDebug()<<this->hasMouseTracking();
  6.     m_timer = new QTimer(this);
  7.     m_timer->start(10000);
  8.     connect(m_timer,SIGNAL(timeout()),this,SLOT(flashSlot()));
  9.     m_btn = new QPushButton(this);
  10.     m_btn->setFixedSize(280,260);
  11.     setStyleSheet("QPushButton{margin:0px 10px 20px 30px;" //top right bottom left
  12.                   "border:10px solid silver;"
  13.                   "border-style: outset;"                                                    
  14.                   "border-radius: 10px;"
  15.                   "padding:5px;"
  16.                   "background-color:gray;"
  17.                   "background-image:url(../1.jpg);"
  18.                   "background-position:top right;"
  19.                   "background-origin:content;"
  20.                   "background-repeat:none;}"
  21.                   "QPushButton:pressed {"
  22.                   "background-color: green;"
  23.                   "border-style: inset;"
  24.                   "}");
  25.     m_btn->setMouseTracking(true);
  26.     m_btn->show();
  27. }

  1. void Widget::flashSlot()  //to set the random color of the button;
  2. {
  3.     m_timer->stop();
  4.     m_timer->start(200);
  5.     int r,g,b;
  6.     QString color,stR,stG,stB;
  7.     qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));
  8.     r = rand() % 256;
  9.     g = rand() % 256;
  10.     b = rand() % 256;
  11.     bool ok = true;
  12.     stR.setNum(r,16);if(stR.toInt(&ok,16)<16){stR.prepend("0");}
  13.     stG.setNum(g,16);if(stG.toInt(&ok,16)<16){stG.prepend("0");}
  14.     stB.setNum(b,16);if(stB.toInt(&ok,16)<16){stB.prepend("0");}
  15.     color.append("#"+stR+stG+stB);
  16.     int r0,g0,b0;
  17.     QString color0,stR0,stG0,stB0;
  18.     qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()));
  19.     r0 = (r+rand()) % 256;
  20.     g0 = (g+rand()) % 256;
  21.     b0 = (b+rand()) % 256;
  22.     stR0.setNum(r0,16);if(stR0.toInt(&ok,16)<16){stR0.prepend("0");}
  23.     stG0.setNum(g0,16);if(stG0.toInt(&ok,16)<16){stG0.prepend("0");}
  24.     stB0.setNum(b0,16);if(stB0.toInt(&ok,16)<16){stB0.prepend("0");}
  25.     color0.append("#"+stR0+stG0+stB0);
  26.     m_btn->setStyleSheet(QString("background-color:%1;border-color:%2;").arg(color,color0));
  27. }

  1. void Widget::mouseMoveEvent(QMouseEvent *e)
  2. {
  3. //    QRect rect(500,500,100,100);
  4.     QRect rect(50,50,100,100);
  5.     if(rect.contains(e->pos()))
  6.     {
  7.         this->setCursor(Qt::OpenHandCursor);
  8.     }
  9.     else
  10.     {
  11.         this->setCursor(Qt::ArrowCursor);
  12.     }
  13.     QWidget::mouseMoveEvent(e);
  14. }


  underside here.rar (13 K) 下载次数:38   are the files of this example.hope that they can help you.
[ 此帖被午小夜在2009-10-30 00:03重新编辑 ]
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
离线hiti_tony
只看该作者 2楼 发表于: 2009-10-30
1楼果然人如其名,半夜来回帖,呵呵。
快速回复
限100 字节
 
上一个 下一个