蛋蛋28群  |  最近更新  |  tag  | 
手机版
蛋蛋28群
当前位置:首页 > qt界面设计

qt界面设计

来源:互联网 时间:2017-07-21 阅读: 手机版

篇一:qt界面设计

1. 子窗体或控件,重写paintevent事件,画边框、背景、文字、图片效果比较好。用paintevent 事件画边框,用qpalette设置背景。http://blog.csdn.net/imxiangzi/article/details/50154951/

// 画边框http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setrenderhint(qpainter::antialiasing, true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setpen(qpen(qcolor(225,70,56), 2));http://blog.csdn.net/imxiangzi/article/details/50154951/

//  painter.setbrush(qcolor(16,62,81));  // 也可设置背景,有时效果不好http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawroundedrect(rect(), 5.0, 5.0);http://blog.csdn.net/imxiangzi/article/details/50154951/

// 设置背景http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setbrush(qpalette::window, qcolor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setpalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  setautofillbackground(true); 继承qdialog,想让对话框以模态运行,使用exec(),继承时使用setwindowflags()后则达不到模态的效果。不知道原因。 qstringlist 输出http://blog.csdn.net/imxiangzi/article/details/50154951/

qstringlist m_strlang english http://blog.csdn.net/imxiangzi/article/details/50154951/

  chinese http://blog.csdn.net/imxiangzi/article/details/50154951/

qdebug( set language: %s , m_strlang.at(0).tolocal8bit().constdata()); qstringlist 使用注意http://blog.csdn.net/imxiangzi/article/details/50154951/

  qstringlist strdateformatlist;http://blog.csdn.net/imxiangzi/article/details/50154951/

  strdateformatlisthttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_strmonth - m_strday - m_stryearhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_strday - m_strmonth - m_stryearhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_stryear - m_strmonth - m_strdayhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  ;http://blog.csdn.net/imxiangzi/article/details/50154951/

如果strdateformatlist 在生命周期内多次调用,则会多次插入字符串,使得字符串重复,这不是我们想要的。那就定义为局部变量,每次会重新赋 ,达到更新的目的。需要动态重新的页面可以在showevent函数中重新。 用paintevent或用调色板为qwidget填充背景时,需要加上setautofillbackground(true);语句,否则没效果。 想使弹出的窗口为圆角,用paintevent画圆角矩形,但四个直角仍然存在,不美观。可以把背景设为透明,用paintevent画出想要的背景,因为四个角没有画上背景,是透明的,所以看不见。http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 设置背景为透明 (构造函数中)http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setbrush(qpalette::window, qcolor(255, 255, 255, 0));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setpalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  setautofillbackground(true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 画背景和边框 (paintevent函数中)http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setrenderhint(qpainter::antialiasing, true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setpen(qpen(qcolor(225,70,56), 2));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setbrush(qcolor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawroundedrect(rect(), 5.0, 5.0); qt::key_enter、qt::key_returehttp://blog.csdn.net/imxiangzi/article/details/50154951/

qt::key_enter对应小键盘的enter键http://blog.csdn.net/imxiangzi/article/details/50154951/

qt::key_return对应大键盘的enter键 事件过滤。窗口部件的事件到达目的地之前提前获取并处理相关事件,实现个性化的处理。http://blog.csdn.net/imxiangzi/article/details/50154951/

我在qlabel对象中放入两个qpushbutton对象和两个qlabel对象,qpushbutton实现 的调节,其中一个qlabel对象用于显示 ,一个qlabel对象用于显示标题。类 spinbox。在多个spinbox上点击实现焦点切换,当鼠标点到qpushbutton上时,qpushbutton处理了mousepressevent事件,所以无法实现焦点切换。为两个qpushbutton对象安装事件过滤器,然后在其父部件中处理eventfilter函数http://blog.csdn.net/imxiangzi/article/details/50154951/

m_btnleft- installeventfilter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

m_btnright- installeventfilter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

//安装事件过滤器后,传给m_btnleft、m_btnright的事件会首先发送给eventfilter函数http://blog.csdn.net/imxiangzi/article/details/50154951/

bool espinboxitem::eventfilter(qobject*obj,qevent*ev)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

if(obj == m_btnleft || obj == m_btnright)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

if(ev- type() == qevent::mousebuttonpress)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

emit sign_spinboxpressed(m_nitem);http://blog.csdn.net/imxiangzi/article/details/50154951/

return true; // 不传给目标对象处理http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

return false; // 不处理的事件则传给目标对象处理http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

10. 拍照动画http://blog.csdn.net/imxiangzi/article/details/50154951/

使用qtimeline和qgraphicspixmapitem对象,当时间线帧改变时,设置qgraphicspixmapitem的位置,实现动画效果。http://blog.csdn.net/imxiangzi/article/details/50154951/

// 在qgraphicsview构造函数中http://blog.csdn.net/imxiangzi/article/details/50154951/

// take photo animationhttp://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timeranimation = new qtimeline(900);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timeranimation- setcurveshape(qtimeline::linearcurve);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_timeranimation- setframerange(0, 240);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_pixmapupanimation = qpixmap( :/images/animation.png http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_pixmapdownanimation = qpixmap( :/images/animation.png http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem = new qgraphicspixmapitem;http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem = new qgraphicspixmapitem;http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- setzvalue(4);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setzvalue(4);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- setpos(0, 0);  // 左上角为原点http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setpos(0, 240);http://blog.csdn.net/imxiangzi/article/details/50154951/

  scene()- additem(m_uppixmapitem);http://blog.csdn.net/imxiangzi/article/details/50154951/

  scene()- additem(m_downpixmapitem);http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- hide();http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- hide();http://blog.csdn.net/imxiangzi/article/details/50154951/

  connect(m_timeranimation, signal(framechanged(int)), this, slot(slot_playanimation(int)));http://blog.csdn.net/imxiangzi/article/details/50154951/

// 槽函数http://blog.csdn.net/imxiangzi/article/details/50154951/

void egraphicsview::slot_playanimation(int nframe)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- show();http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- show();http://blog.csdn.net/imxiangzi/article/details/50154951/

  qdebug( nframe: %d , nframe);http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(nframe 120)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- setpixmap(m_pixmapupanimation.scaled(320, nframe));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setpixmap(m_pixmapdownanimation.scaled(320, nframe));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setpos(0, 240 - nframe);http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  elsehttp://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_uppixmapitem- setpixmap(m_pixmapupanimation.scaled(320, 240 - nframe));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setpixmap(m_pixmapdownanimation.scaled(320, 240 - nframe));http://blog.csdn.net/imxiangzi/article/details/50154951/

  m_downpixmapitem- setpos(0, nframe);http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

11.在移动设备上使用渐变或使用渐变的图片时,会出现条纹状,达不到效果。在初始化qapplication对像前调用下面的语句则可以达到效果:http://blog.csdn.net/imxiangzi/article/details/50154951/

qapplication::setcolorspec(qapplication::manycolor); //the right choice for applications that use thousands of colorshttp://blog.csdn.net/imxiangzi/article/details/50154951/

qapplication app(argc, argv,  qapplication::guiserver );http://blog.csdn.net/imxiangzi/article/details/50154951/

12.用setstylesheet设置背景图片http://blog.csdn.net/imxiangzi/article/details/50154951/

m_labelbutton- setstylesheet( background-image: url(/jiaoyukexue/:images/shutdown/slider.png); background-repeat: no-repeat; //注意,不加background-repeat: no-repeat可能会出现透视,很丑。qlabel对象可用setpixmap设置象素图。http://blog.csdn.net/imxiangzi/article/details/50154951/

13.使对话框圆角http://blog.csdn.net/imxiangzi/article/details/50154951/

  //构造函数中http://blog.csdn.net/imxiangzi/article/details/50154951/

  setautofillbackground(true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpalette pal = palette();http://blog.csdn.net/imxiangzi/article/details/50154951/

  pal.setcolor(qpalette::window, qcolor(0, 0, 0, 0));http://blog.csdn.net/imxiangzi/article/details/50154951/

  setpalette(pal);http://blog.csdn.net/imxiangzi/article/details/50154951/

  //paintevent函数中http://blog.csdn.net/imxiangzi/article/details/50154951/

  qpainter painter(this);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setrenderhint(qpainter::antialiasing,true);http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setpen(qpen(qt::red,2));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.setbrush(qcolor(16,62,81));http://blog.csdn.net/imxiangzi/article/details/50154951/

  painter.drawroundedrect(rect(),5.0,5.0);http://blog.csdn.net/imxiangzi/article/details/50154951/

// 圆角处有杂色,原因不祥http://blog.csdn.net/imxiangzi/article/details/50154951/

14. 对某些默认事件的处理修改http://blog.csdn.net/imxiangzi/article/details/50154951/

正常情况下,按下tab键,被qwidget看成是去移动键盘焦点,但少数窗口部件需要自行解释。http://blog.csdn.net/imxiangzi/article/details/50154951/

bool myclass::event(qevent *e)http://blog.csdn.net/imxiangzi/article/details/50154951/

{http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(e- type() == qevent::keypress)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  qkeyevent *ke = (qkeyevent *)e;http://blog.csdn.net/imxiangzi/article/details/50154951/

  if(ke- key() == qt::key_tab)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  //这里是特定的tab处理http://blog.csdn.net/imxiangzi/article/details/50154951/

  k- accept();http://blog.csdn.net/imxiangzi/article/details/50154951/

  return true;http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  else if(e- type()  = qevent::user)http://blog.csdn.net/imxiangzi/article/details/50154951/

  {http://blog.csdn.net/imxiangzi/article/details/50154951/

  // 这里是自定义事件处理http://blog.csdn.net/imxiangzi/article/details/50154951/

  return true;http://blog.csdn.net/imxiangzi/article/details/50154951/

  }http://blog.csdn.net/imxiangzi/article/details/50154951/

  qwidget::event(e);http://blog.csdn.net/imxiangzi/article/details/50154951/

}http://blog.csdn.net/imxiangzi/article/details/50154951/

  15.qlabel继承qframe,有qframe的特性。如setframestyle(int)可设置qlabel的framestyle(框架类型)http://blog.csdn.net/imxiangzi/article/details/50154951/

framestyle有两种:http://blog.csdn.net/imxiangzi/article/details/50154951/

a.第一种是frameshape:http://blog.csdn.net/imxiangzi/article/details/50154951/

有以下几种类型:http://blog.csdn.net/imxiangzi/article/details/50154951/

constanthttp://blog.csdn.net/imxiangzi/article/details/50154951/

valuehttp://blog.csdn.net/imxiangzi/article/details/50154951/

descriptionhttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::noframehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws nothinghttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::boxhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0001http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a box around its contentshttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::panelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0002http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a panel to make the contents appear raised or sunkenhttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::styledpanelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0006http://blog.csdn.net/imxiangzi/article/details/50154951/

draws a rectangular panel with a look that depends on the current gui style. it can be raised or sunken.http://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::hlinehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0004http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a horizontal line that frames nothing (useful as separator)http://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::vlinehttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0005http://blog.csdn.net/imxiangzi/article/details/50154951/

 draws a vertical line that frames nothing (useful as separator)http://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::winpanelhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0003http://blog.csdn.net/imxiangzi/article/details/50154951/

draws a rectangular panel that can be raised or sunken like those in windows 95. specifying this shape sets the line width to 2 pixels. winpanel is provided for compatibility. for gui style independence we recommend using styledpanel instead.http://blog.csdn.net/imxiangzi/article/details/50154951/

b.第二种是frameshadowhttp://blog.csdn.net/imxiangzi/article/details/50154951/

有以下几种类型:http://blog.csdn.net/imxiangzi/article/details/50154951/

constanthttp://blog.csdn.net/imxiangzi/article/details/50154951/

valuehttp://blog.csdn.net/imxiangzi/article/details/50154951/

descriptionhttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::plainhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0010http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear level with the surroundings; draws using the palettecolor (without any 3d effect)http://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::raisedhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0020http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear raised; draws a 3d raised line using the light and dark colors of the current color grouphttp://blog.csdn.net/imxiangzi/article/details/50154951/

qframe::sunkenhttp://blog.csdn.net/imxiangzi/article/details/50154951/

0x0030http://blog.csdn.net/imxiangzi/article/details/50154951/

the frame and contents appear sunken; draws a 3d sunken line using the light and dark colors of the current color grouphttp://blog.csdn.net/imxiangzi/article/details/50154951/

组合 的效果图:http://blog.csdn.net/imxiangzi/article/details/50154951/

  lineedit =new qlineedit;

 label- setbuddy(lineedit);

  casecheckbox= new qcheckbox(tr("match case"));

 backwardcheckbox = new qcheckbox(tr("search backward"));

  finon =new qpushbutton(tr(" find"));

 finon- setdefault(true);

 finon- setenabled(false);

  closebutton= new qpushbutton(tr("close"));

 connect(lineedit, signal(textchanged(const qstring )),

 this, slot(enablefinon(const qstring )));

 connect(finon, signal(clicked()),

 this, slot(findclicked()));

 connect(closebutton, signal(clicked()),

 this, slot(close()));

  //使用布局管理器布局控件

  qhboxlayout*topleftlayout = new qhboxlayout;

 topleftlayout- addwidget(label);

 topleftlayout- addwidget(lineedit);

  qvboxlayout*leftlayout = new qvboxlayout;

 leftlayout- addlayout(topleftlayout);

 leftlayout- addwidget(casecheckbox);

 leftlayout- addwidget(backwardcheckbox);

  qvboxlayout*rightlayout = new qvboxlayout;

 rightlayout- addwidget(finon);

 rightlayout- addwidget(closebutton);

 rightlayout- addstretch();

  qhboxlayout*mainlayout = new qhboxlayout;

 mainlayout- addlayout(leftlayout);

 mainlayout- addlayout(rightlayout);

  //设置窗口的布局管理器

 setlayout(mainlayout); 

 setwindowtitle(tr("find"));

 setfixedheight(sizehint().height());

}

使用qt designer设计界面 采用qt designer,使得快速创建对话框成为可能。在qtdesigner环境中,所有的操作都采用可视化的操作,可拖放控件、关联信号与槽、设置特定控件的属性。使用qtdesigner设计界面的方法如下图所示:

 

猜你喜欢

最新文章

热门文章