Results 1 to 1 of 1

Thread: trying to get a QIcon into the center of a QTableWidgetItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default trying to get a QIcon into the center of a QTableWidgetItem

    icons seem to only show up on the left within the item.

    I've tried overriding paint() within my attached QStyledItemDelegate derived class, but it still only shows on the left.

    Within my table, when a column is showing an icon, I need it to occupy the center of the whole QTableWidgetItem (cell).

    Below is my, uhhh, not very pretty code.

    My main code populating the table has a callback func to populate QComboBoxs and other stuff.
    QComboBoxes are the main reason I add the SIDlg class derived from QStyledItemDelegate.

    I -think- It's just the paint() method within class SIDlg that needs some work.
    But I don't know how to get the whole rectangle (I think that's the problem??)

    Qt Code:
    1. // Populating table:
    2.  
    3. // my main window class has a
    4. CtlTabl _tr;
    5.  
    6. // window class init function has this hooking design time tr which is a QTableWidget
    7. // column headers in a zz string
    8. // * prefix means show an icon. _ prefix for edited column. ^ prefix means QComboBox edited > means right justified
    9. _tr.Init (ui->tr,
    10. "*Lrn\0"
    11. "*^Hand\0"
    12. "_Track\0"
    13. "^SoundDir\0"
    14. "^Sound\0"
    15. "Dev.Chan\0"
    16. "Mix\0"
    17. ">Notes\0"
    18. ">Ctrls\0", TrPop);
    19. _tr.SetRowH (Up.txH - 4);
    20.  
    21. ui->tr->setContextMenuPolicy (Qt::CustomContextMenu);
    22. connect (ui->tr, & QTableWidget::itemClicked, this, & PCheetah::TrClk);
    23. connect (ui->tr, & QTableWidget::customContextMenuRequested,
    24. this, & PCheetah::TrClkR);
    25. connect (ui->tr, & QTableWidget::itemChanged, this, & PCheetah::TrUpd);
    26.  
    27.  
    28. // Then populated as:
    29.  
    30. char *rp [32];
    31. _tr.Open ();
    32. for (ubyte i = 0, tc = 0; i < Up.rTrk; i++) {
    33. rp [0] = Up.trk [i].lrn; rp [1] = Up.trk [i].ez;
    34. rp [2] = Up.trk [i].name; rp [3] = Up.trk [i].grp;
    35. rp [4] = Up.trk [i].snd; rp [5] = Up.trk [i].dev;
    36. rp [6] = CC(""); rp [7] = Up.trk [i].notes;
    37. rp [8] = Up.trk [i].ctrls; rp [9] = nullptr;
    38. _tr.Put (rp);
    39. if (Cfg.ntCo == 2) { // color by track
    40. if ((rp [0][0] == 'l') || (rp [1][0] == 'S'))
    41. _tr.SetColor (i, CMap (tc++));
    42. }
    43. else {
    44. switch (rp [1][0]) {
    45. case 'L': tc = 0; break;
    46. case 'R': tc = 1; break;
    47. default: tc = 9;
    48. }
    49. if (tc < 2) _tr.SetColor (i, CTnt [tc]);
    50. }
    51.  
    52. }
    53. _tr.Shut (); _tr.HopTo (Up.eTrk, 0);
    54.  
    55.  
    56.  
    57.  
    58. typedef void (*ppop)(char *ls, ubyt2 r, ubyte c);
    59.  
    60. class SIDlg: public QStyledItemDelegate {
    61. Q_OBJECT
    62. private:
    63. char *_ed;
    64. ppop _pop;
    65. QTableWidget *_tbl;
    66.  
    67. public:
    68. SIDlg (QObject *par, char *ed, ppop pop);
    69. ~SIDlg () {}
    70. void paint (QPainter *p, const QStyleOptionViewItem &opt,
    71. const QModelIndex &ind) const override;
    72. QWidget *createEditor (QWidget *tb, const QStyleOptionViewItem &opt,
    73. const QModelIndex &ind) const override;
    74. void setEditorData (QWidget *ed, const QModelIndex &ind) const override;
    75. void setModelData (QWidget *ed, QAbstractItemModel *mod,
    76. const QModelIndex &ind) const override;
    77. public slots:
    78. void cbChanged (int i);
    79. };
    80.  
    81.  
    82. class CtlTabl {
    83. public:
    84. CtlTabl () {_t = nullptr; _nr = _tr = _ih = _tc = 0;}
    85. ~CtlTabl () {}
    86.  
    87. // hdr is zz string of labels
    88. // >| prefix means right or center just
    89. // _^ prefix means string or combo edit
    90. void Init (QTableWidget *t, const char *hdr, ppop pop = nullptr);
    91. void SetRowH (ubyt2 h);
    92. ubyt2 ColW (ubyte c);
    93. void SetColW (ubyte c, ubyt2 w);
    94. ubyt2 NRow ();
    95. ubyte NCol ();
    96. ubyt2 CurRow ();
    97. ubyte CurCol ();
    98. char *Get (ubyt2 r, ubyte c);
    99. void Set (ubyt2 r, ubyte c, char *s);
    100. void HopTo (ubyt2 r, ubyte c);
    101. void SetColor (ubyt2 r, QColor c);
    102.  
    103. void Open ();
    104. void Put (char **rp);
    105. void Shut ();
    106.  
    107. private:
    108. char _ju [40];
    109. char _ed [40];
    110. ubyt2 _nr, _tr, _ih;
    111. ubyte _tc;
    112. };
    113.  
    114.  
    115. SIDlg::SIDlg (QObject *par, char *ed, ppop pop)
    116. : QStyledItemDelegate (par)
    117. { _ed = ed; _pop = pop; }
    118.  
    119. void SIDlg::paint (QPainter *p, const QStyleOptionViewItem &opt,
    120. const QModelIndex &ind) const
    121. { if (! opt.icon.isNull ())
    122. {p->save (); opt.icon.paint (p, opt.rect);
    123. p->restore ();}
    124. else QStyledItemDelegate::paint (p, opt, ind);
    125. }
    126.  
    127. void SIDlg::cbChanged (int i)
    128. { QComboBox *cb = qobject_cast<QComboBox *>(sender ());
    129. (void)i; emit commitData (cb); emit closeEditor (cb);
    130. }
    131.  
    132. QWidget *SIDlg::createEditor (QWidget *par, const QStyleOptionViewItem &opt,
    133. const QModelIndex &ind) const
    134. { BStr bs;
    135. char *s;
    136. if (_ed [ind.column ()] == '^') {
    137. _pop (bs, ind.row (), ind.column ());
    138. if (*bs == 0) return nullptr;
    139.  
    140. QComboBox *cb = new QComboBox (par);
    141. for (s = bs; *s; s = & s [StrLn (s)+1])
    142. cb->addItem (StrCm (s, CC("-")) ? s : "");
    143. return cb;
    144. }
    145. return QStyledItemDelegate::createEditor (par, opt, ind);
    146. }
    147.  
    148. void SIDlg::setEditorData (QWidget *ed, const QModelIndex &ind) const
    149. { QComboBox *cb = qobject_cast<QComboBox *>(ed);
    150. if (cb) {
    151. int i = cb->findText (ind.data (Qt::EditRole).toString ());
    152. if (i < 0) i = 0;
    153. cb->setCurrentIndex (i); cb->showPopup ();
    154. connect (cb, & QComboBox::currentIndexChanged,
    155. this, & SIDlg::cbChanged);
    156. }
    157. else QStyledItemDelegate::setEditorData (ed, ind);
    158. }
    159.  
    160. void SIDlg::setModelData (QWidget *ed, QAbstractItemModel *mod,
    161. const QModelIndex &ind) const
    162. { QComboBox *cb = qobject_cast<QComboBox *>(ed);
    163. if (cb) mod->setData (ind, cb->currentText (), Qt::EditRole);
    164. else QStyledItemDelegate::setModelData (ed, mod, ind);
    165. }
    166.  
    167.  
    168. void CtlTabl::Init (QTableWidget *t, const char *hdr, ppop pop)
    169. // hdr is zz string of labels
    170. // * prefix means icon
    171. // >| prefix means right or center just
    172. // _^ prefix means string or combo edit
    173. { ubyte c;
    174. char *h;
    175. char ed = '\0'; // _ means editing, ^ means QComboBox so delegate too
    176. _t = t;
    177. for (c = 0, h = CC(hdr); *h; c++, h = & h [StrLn (h)+1]) {
    178. _ju [c] = _ed [c] = '\0';
    179. if ((*h == '*') || (*h == '>') || (*h== '|')) _ju [c] = *h++;
    180. if (*h == '_') {_ed [c] = *h++; if (ed != '^') ed = '_';}
    181. if (*h == '^') _ed [c] = ed = *h++;
    182. sl << QString::fromStdString (h);
    183. }
    184. _t->horizontalHeader ()->setSectionResizeMode (
    185. QHeaderView::ResizeToContents);
    186. _t->setColumnCount (c); _t->setHorizontalHeaderLabels (sl);
    187. _t->verticalHeader ()->hide ();
    188. _t->setAlternatingRowColors (false);
    189. if (ed == '^') _t->setItemDelegate (new SIDlg (_t, _ed, pop));
    190. _t->setEditTriggers (ed ? QAbstractItemView::AllEditTriggers
    191. : QAbstractItemView::NoEditTriggers);
    192. _t->setSelectionBehavior (QAbstractItemView::SelectRows);
    193. }
    194.  
    195. /* _t->horizontalHeader ()->setSectionResizeMode (2, QHeaderView::Fixed)
    196. ** _t->horizontalHeader ()->setStretchLastSection (true);
    197. ** _t->horizontalHeader ()->setHighlightSections (false);
    198. ** _t->setSelectionBehavior (QAbstractItemView::SelectItems/Rows);
    199. ** _t->setSelectionMode (QAbstractItemView::ExtendedSelection);
    200. ** _t->setGridShow (false);
    201. */
    202.  
    203. void CtlTabl::SetRowH (ubyt2 h)
    204. { _t->verticalHeader ()->setDefaultSectionSize (h);
    205. _t->setIconSize (QSize (h, h));
    206. _ih = h;
    207. }
    208.  
    209. ubyt2 CtlTabl::ColW (ubyte c) {return _t->columnWidth (c);}
    210. void CtlTabl::SetColW (ubyte c, ubyt2 w)
    211. { _t->horizontalHeader ()->setSectionResizeMode (c, QHeaderView::Fixed);
    212. _t->setColumnWidth (c, w);
    213. }
    214.  
    215. ubyt2 CtlTabl::NRow () {return _t->rowCount ();}
    216. ubyte CtlTabl::NCol () {return _t->columnCount ();}
    217. ubyt2 CtlTabl::CurRow () {return _t->currentRow ();}
    218. ubyte CtlTabl::CurCol () {return _t->currentColumn ();}
    219. char *CtlTabl::Get (ubyt2 r, ubyte c) {return UnQS (_t->item (r, c)->text ());}
    220.  
    221. // clip cuz too many chars to post
    222.  
    223. void CtlTabl::Open ()
    224. { _tr = CurRow (); _tc = CurCol ();
    225. _t->hide (); _t->blockSignals (true);
    226. _t->clearContents (); _t->setRowCount (0);
    227. _nr = 0;
    228. }
    229.  
    230. void CtlTabl::Put (char **rp)
    231. { ubyte c;
    232. TStr ico;
    233. _t->setRowCount (_nr+1);
    234. for (c = 0; *rp; c++, rp++) {
    235. if (! (it = _t->item (_nr, c)))
    236. _t->setItem (_nr, c, it = new QTableWidgetItem);
    237. if (_ed [c]) it->setFlags (it->flags () | Qt::ItemIsEditable);
    238. else it->setFlags (it->flags () & ~Qt::ItemIsEditable);
    239. if (_ju [c] == '*') {
    240. if (**rp) {
    241. if (**rp == '*') it->setText (++(*rp));
    242. else if (**rp) it->setIcon (QIcon (StrFmt (ico,
    243. ":/tico/`s", *rp)));
    244. else it->setIcon (QIcon ());
    245. }
    246. }
    247. else {
    248. if (_ju [c] == '>') it->setTextAlignment (Qt::AlignRight);
    249. if (_ju [c] == '|') it->setTextAlignment (Qt::AlignCenter);
    250. it->setText (*rp);
    251. }
    252. }
    253. _nr++;
    254. }
    255.  
    256. void CtlTabl::Shut ()
    257. { _t->show (); _t->blockSignals (false); HopTo (_tr, _tc); }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 21st October 2021 at 17:44. Reason: Removed inappropriate FONT tags.

Similar Threads

  1. Replies: 13
    Last Post: 17th July 2016, 17:20
  2. Replies: 1
    Last Post: 4th April 2011, 20:41
  3. Qicon
    By shihao in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2010, 14:23
  4. How to get the QIcon fileneme in use.
    By gt.beta2 in forum Newbie
    Replies: 1
    Last Post: 25th February 2009, 16:30
  5. Replies: 2
    Last Post: 31st May 2006, 22:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.