I have QListView, which has a model whose model is QSqlQueryModel. How can I use a QStyledItemDelegate in order to customize the appearance of QListView's appearance (for example, show 2 text lines)
QSqlDatabase db = QSqlDatabase :: addDatabase ("QSQLITE" ); Db.setDatabaseName ("test.db"); If (! Db.open ()) {qDebug () & lt; & Lt; Db.lastError (); QFatal ("Failed to connect."); } QDebug ("Connected!"); QSqlQueryModel * sqlModel = New QSqlQueryModel; SqlModel-> Setquan ("selection from * fROM entries"); MListWidget- & gt; SetModel (sqlModel); Essentially, what do I think is that in some way to "match" the roles in the areas of DB table, in order to be able to get data from QStyledItemDelegate, something like this Using the following: Zero ListViewDelegate :: Color (QPainter * Painters, Constants QStyleOptionViewItem & Options, Constants QModelIndex & Indexes) Constants {[...] QString headerText = qvariant_cast & LT; QString & gt; (Index.data (headerRole)); QString subtext = qvariant_cast & lt; QString & gt; (Index.data (subHeaderRole)); [...]} Thanks! You can of course use QStyledItemDelegate for drawing custom items. QModelIndex has a reference to the model object, which you can use to obtain the "entries" record field. If you need to show more data, you will need to redefine the model's method to increase the size of an item to a single data. The other is that it is more or less trivial, option, QModelIndex & amp; Zero Color (QPainter * Illustrator, Constant QStyleOptionViewItem & amp; ListViewDelegate: Public QStyledItemDelegate {Protected Pls, see if an example below will work you; index) const {QStyleOptionViewItemV4 opt = option; InitStyleOption (& opt, index); QString line0 = index.model () - & gt; Data (index.model () -> index (index.row (), 1)). ToString (); QString line1 = index.model () - & gt; Data (index.model () -> index (index.row (), 2)). ToString (); // Choose the right background. Opt.text = ""; QStyle * style = opt.widget? Opt.widget-> Style (): QApplication :: style (); Style- & gt; Draw Control (QStyle :: CE_ItemViewItem, and Opt, Painter, opt.widget); QRect rect = opt.rect; Copala :: Color Group CG = OptState & amp; QStyle :: State_Enabled? QPalette :: General: QPalette :: disabled; If (cg == QPalette :: Normal & amp; (Opt.state and QStyle :: State_Active)) cg = QPalette :: Inactive; // Set Color Color If (opt.state & QStyle :: State_Selected) painter- & gt; SetPen (opt.palette.color (cg, QPalette :: HighlightedText)); Other painters- & gt; Setpane (opt.palette.color (cg, cuplet :: text)); DrawText (QRect (rect.left (), rect.top (), rect.width (), rect.height () / 2), opt.displayAlignment, line0); // draw 2 lines of text-painter-> Painter- & gt; DrawText (QRect (rect.left (), rect.top () + rect.height () / 2, rect.width (), rect.height () / 2), opt.displayAlignment, line 1); } QSize sizeHint (const QStyleOptionViewItem and options, const QModelIndex and index) const {QSize results = QStyledItemDelegate :: sizeHint (option, index); Result.setHeight (result.height () * 2); Return result; }};
The test database set is defined here:
QSqlError initDb () {QSqlDatabase db = QSqlDatabase :: addDatabase ("QSQLITE"); Db.setDatabaseName (": memory:"); If (! Db.open ()) return db.lastError (); QStringList tables = db.tables (); Consult the QSqlError (); QSqlQuery q; if (! Q.exec (QLatin1String ("table entries (ID integer primary key, first_line varchar, second_line varchar)")) (Consult the tables ("Test", QT :: case descriptive) ) Return q.lastError (); Q.exec (enter the entries (id, first_line, second_line) values (0, 'first line 0', 'second line 0')); Q.exec ("entries (IDs, First_line, second_line) values (1, 'first line 1', 'second line 1') "); Q.exec (" entry Enter the values (id, first_line, seconds_line) values (2, 'first line 2', 'second line 2')); Return QSqlError ();} Model and listview definition :
initDb (); QSqlQueryModel * sqlModel = new QSqlQueryModel (); SqlModel- & gt; SetQayon ("Selection from * FROM entries"); UI-> ListView- & gt; SetModel (sqlModel); UI-> List view-> Set ITEM Delegate (new listviewedly); Hope this helps, < / Div>
Comments
Post a Comment