Home > Back-end >  QTableWidget form right-click menu
QTableWidget form right-click menu

Time:05-12

,, can you tell me the QTableWidget form the first two lines show a prompt, hidden under the header, and display to display the content of the following screenshots:

Specific code is as follows:
The class CustomTableWidget: public QTableWidget
{
Q_OBJECT
Public:
Explicit CustomTableWidget (QWidget * parent=nullptr);
~ CustomTableWidget ();
Private:
QTableWidget * m_pTableWidget;
};

//CustomTableWidget constructor
M_pTableWidget=new QTableWidget (this);
M_pTableWidget - & gt; The resize (this - & gt; Width () * 90/100, this - & gt; Height () * 80/100);
M_pTableWidget - & gt; The horizontalHeader () - & gt; SetVisible (false);//header is not visible
M_pTableWidget - & gt; The verticalHeader () - & gt; SetVisible (false);//header is not visible
M_pTableWidget - & gt; SetShowGrid (false);//grid lines invisible
M_pTableWidget - & gt; SetEditTriggers (QAbstractItemView: NoEditTriggers);//set the cell cannot edit
M_pTableWidget - & gt; The horizontalHeader () - & gt; SetStretchLastSection (true);//in the end, the expanded
a cellM_pTableWidget - & gt; SetFocusPolicy (Qt: : NoFocus);//solve the problem of selected gridlines
M_pTableWidget - & gt; SetFrameShape (QFrame: : NoFrame);//remove border embarrassing
M_pTableWidget - & gt; SetVerticalScrollBarPolicy (Qt: : ScrollBarAlwaysOff);//hide the scroll bar
M_pTableWidget - & gt; SetHorizontalScrollBarPolicy (Qt: : ScrollBarAlwaysOff);
M_pTableWidget - & gt; SetHorizontalScrollMode (ScrollPerPixel);

M_pTableWidget - & gt; SetItemDelegate (new CustomItemDelegate (0));

Viewport () - & gt; StackUnder (m_pTableWidget);

M_pTableWidget - & gt; SetColumnCount (4);//set the number of columns
M_pTableWidget - & gt; SetRowCount (2);//set the number of rows as header

M_pTableWidget - & gt; SetRowHeight (0, 20);//first line set up 42 px height
M_pTableWidget - & gt; SetRowHeight (1, 20);//the second line setting height of 42 px

For (int row=2; The row & lt; M_pTableWidget - & gt; RowCount (); + + row)//hide 2 line after line
M_pTableWidget - & gt; SetRowHidden (row, true);

//merge cells
M_pTableWidget - & gt; SetSpan (0, 0, 2, 4);
M_pTableWidget - & gt; SetItem (0, 0, new QTableWidgetItem (QString (" please note that XXX ")));


SetStyleSheet (QString: : fromUtf8 (" border: 1 px solid # 003 da6; Border - the radius: 1 px;" ));
ClearContents ();
SetColumnCount (4);

The horizontalHeader () - & gt; SetVisible (false);//header is not visible
Int headerHeight=m_pTableWidget - & gt; RowHeight (0) + m_pTableWidget - & gt; RowHeight (1);
The horizontalHeader () - & gt; SetFixedHeight (headerHeight);
The verticalHeader () - & gt; SetVisible (false);//header is not visible
SetShowGrid (false);//grid lines invisible
SetEditTriggers (QAbstractItemView: NoEditTriggers);//set the cell cannot edit
SetSelectionMode (QAbstractItemView: : SingleSelection);//radio
SetSelectionBehavior (QAbstractItemView: SelectRows);//selected lines
The horizontalHeader () - & gt; SetStretchLastSection (true);//in the end, the expanded
a cellSetFocusPolicy (Qt: : NoFocus);//solve the problem of selected gridlines
SetFrameShape (QFrame: : NoFrame);//remove border
SetVerticalScrollBarPolicy (Qt: : ScrollBarAlwaysOff);//hide the scroll bar
SetHorizontalScrollBarPolicy (Qt: : ScrollBarAlwaysOff);
SetVerticalScrollMode (ScrollPerPixel);
SetHorizontalScrollMode (ScrollPerPixel);

SetItemDelegate (new CustomItemDelegate (1));

//CustomItemDelegate agent is mainly written to distinguish the header and table body

QTableWidget code above there are two main form, one of the QTableWidget is used as headers, main is to merge header display text, another table is QTableWidget derived classes, is mainly used to display the tables in the body, the two forms, the main display form of the area how to prohibit right-click menu? I'm calling QTableWidget derived class objects, created the right-click menu, the code is as follows:
CustomTableWidget * m_cTableWidget=NULL;
M_cTableWidget - & gt; SetContextMenuPolicy (Qt: : CustomContextMenu);
The connect (m_cTableWidget, & amp; M_cTableWidget: : customContextMenuRequested, this, & amp; MyWidget: : slotCustomContextMenuRequested);
M_cTableWidget - & gt; SetAcceptDrops (false);
This - & gt; SetAcceptDrops (true);
But also shows the menu in the header of the area, how to ban it, according to the body content area in the table shows only the menu? Online and so on, welcome each big, the younger brother be appreciated here!
  • Related