Briefly
Extension of the previous chapters, we have implemented the Qtableview data display, custom sorting, display check box, progress bar and other functions of the implementation, this section is mainly for the custom button to explain, after this section, but also hope that you have a more in-depth understanding of the customization, in the future function development process, We believe that whatever style we encounter, we can achieve it well.
- Briefly
- Effect
- Qstyleditemdelegate
- Yan Extension
Effect
Qstyleditemdelegate Source
. h
Contains smart pointers to display buttons, width, height, spacing between buttons, mouse coordinates, and so on.
Class Tableviewdelegate: Publicqstyleditemdelegate{Q_object Public:Explicit tableviewdelegate(Qwidget *parent =0); ~tableviewdelegate ();voidPaint (Qpainter *painter,ConstQstyleoptionviewitem &option,ConstQmodelindex &index)Const;BOOLEditorevent (qevent*Event, qabstractitemmodel* model,Constqstyleoptionviewitem& option,Constqmodelindex& index); Signals:voidOpenConstQmodelindex &index);voidDeleteData (ConstQmodelindex &index);Private: Qpoint M_mousepoint;//Mouse positionQscopedpointer<qpushbutton> M_popenbutton; Qscopedpointer<qpushbutton> M_pdeletebutton; Qstringlist m_list;intm_nspacing;//The spacing between buttons intM_nwidth;//Button width intM_nheight;//Button height intM_ntype;//Button state-1: Across 2: Press};
. cpp
Mainly set the button style, to achieve mouse across, press, respond to mouse events and other actions.
Tableviewdelegate::tableviewdelegate (Qwidget *parent): qstyleditemdelegate (parent), M_popenbutton (NewQpushbutton ()), M_pdeletebutton (NewQpushbutton ()), m_nspacing (5), M_nwidth ( -), M_nheight ( -){//Set button to normal, across, press styleM_popenbutton->setstylesheet ("Qpushbutton {border:none; Background-color:transparent; Image:url (:/images/open);} Qpushbutton:hover {Image:url (:/images/openhover);} qpushbutton:pressed {Image:url (:/images/openpressed);} "); M_pdeletebutton->setstylesheet ("Qpushbutton {border:none; Background-color:transparent; Image:url (:/images/delete);} Qpushbutton:hover {Image:url (:/images/deletehover);} qpushbutton:pressed {Image:url (:/images/deletepressed);} "); M_list << Qstringliteral ("Open") << Qstringliteral ("Delete");} Tableviewdelegate::~tableviewdelegate () {}//Draw buttonvoidTableviewdelegate::p aint (Qpainter *painter,ConstQstyleoptionviewitem &option,ConstQmodelindex &index)Const{qstyleoptionviewitem viewoption (option); Initstyleoption (&viewoption, index);if(Option.state.testFlag (qstyle::state_hasfocus)) Viewoption.state = viewoption.state ^ qstyle::state_hasfocus; Qstyleditemdelegate::p aint (painter, viewoption, index);if(Index.column () = = File_operate_column) {//Calculate button display area intncount = M_list.count ();intNhalf = (Option.rect.width ()-M_nwidth * ncount-m_nspacing * (ncount-1)) /2;intNTop = (Option.rect.height ()-m_nheight)/2; for(inti =0; i < ncount; ++i) {//Draw buttonQstyleoptionbutton button; Button.rect = Qrect (Option.rect.left () + nhalf + m_nwidth * i + m_nspacing * I, Option.rect . Top () + NTop, m_nwidth, m_nheight); Button.state |= qstyle::state_enabled;//button.iconsize = qsize (+--); //button.icon = Qicon (QString (":/images/%1"). Arg (m_list.at (i))); if(Button.rect.contains (M_mousepoint)) {if(M_ntype = =0) {button.state |= qstyle::state_mouseover;//button.icon = Qicon (QString (":/images/%1hover"). Arg (m_list.at (i)));}Else if(M_ntype = =1) {button.state |= qstyle::state_sunken;//button.icon = Qicon (QString (":/images/%1pressed"). Arg (m_list.at (i)));}} qwidget *pwidget = (i = =0) ? M_popenbutton.data (): M_pdeletebutton.data (); Qapplication::style ()->drawcontrol (Qstyle::ce_pushbutton, &button, painter, pwidget); } }}//Response button event-across, pressBOOLTableviewdelegate::editorevent (qevent* event, qabstractitemmodel* model,Constqstyleoptionviewitem& option,Constqmodelindex& index) {if(Index.column ()! = File_operate_column)return false; M_ntype =-1;BOOLBrepaint =false; Qmouseevent *pevent =static_cast<qmouseevent *> (event); M_mousepoint = Pevent->pos ();intncount = M_list.count ();intNhalf = (Option.rect.width ()-M_nwidth * ncount-m_nspacing * (ncount-1)) /2;intNTop = (Option.rect.height ()-m_nheight)/2;//restore mouse styleQapplication::restoreoverridecursor (); for(inti =0; i < ncount; ++i) {Qstyleoptionbutton button; Button.rect = Qrect (Option.rect.left () + nhalf + m_nwidth * i + m_nspacing * I, Option.rect.top () + NTop, m_nwidth, m_nheight);//Mouse on button if(!button.rect.contains (M_mousepoint))Continue; Brepaint =true;Switch(Event->type ()) {//mouse slide over CaseQevent::mousemove: {//Set mouse style to hand typeQapplication::setoverridecursor (Qt::P ointinghandcursor); M_ntype =0; Qtooltip::showtext (Pevent->globalpos (), m_list.at (i)); Break; }//mouse button down CaseQevent::mousebuttonpress: {M_ntype =1; Break; }//Mouse release CaseQevent::mousebuttonrelease: {if(i = =0) {Emit open (index); }Else{Emit deleteData (index); } Break; }default: Break; } }returnBrepaint;}
Yan Extension
Through the above implementation, we can customize the style of the button, text, display area, etc., we can also through the Qstyleoptionbutton icon and iconsize to set the button icons and icon size, through the response button to achieve our own events.
QT model/view (custom button)