Qt Item Delegates - A Framework

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.ค. 2024
  • QListView, QTableView and QTreeView can all be configured via a number of roles from the models, e.g. BackgroundRole, ForegroundRole, FontRole, and TextAlignmentRole. This often means that no additional configurations of the views are needed.
    However, if you want more control over how the data is presented, you can get that by implementing a delegate.
    The question, however, is: Which delegate should you subclass from - QItemDelegate or QStyledItemDelegate? The documentation argues the latter, but the answer isn't so black and white. In this video I'll look into many details, and at the end implement a new super class which is super easy to extend.
    0:00 Introduction
    0:47 When do you /not/ need a delegate?
    2:33 Rendering Money instances in a delegate without a custom delegate
    4:21 Difference between QItemDelegate and QStyledItemDelegate
    6:56 QStyledItemDelegate with Oxygen style
    7:59 Subclassing QStyledItemDelegate
    11:00 Debugging into QStyledItemDelegate
    15:00 Subclassing QItemDelegate
    19:06 Paint negative numbers in red
    20:55 QWAMDelegate
    22:49 What's in the next two episodes?
    Links:
    * Avoid wrapping the money instance in QVariant::fromValue() - • Using Custom Types wit...
    The example code showcased in this video is available here: github.com/KDAB/kdabtv/tree/m...
    All Qt Widgets and more videos:
    • Qt Widgets and more
    About the host:
    The video is presented by Jesper Pedersen, who started with Qt back when Qt was widgets only and the version was 1.44, which gives him 21 years of experience with Qt. Since 2000 he has taught almost 100 Qt classes, ranging from Qt Widgets to QML. Heck, Jesper even taught a few classes on QTopia Core - Trolltech's initial attempt at creating a phone stack. Today Jesper holds a fancy business title, but that doesn't prevent him from coding both in his job and in his spare time.
    Every month our newsletter is featuring technical blogs on Qt, C++ and 3D topics: www.kdab.com/development-reso...
    About KDAB:
    KDAB offers experienced software experts to help you deliver functional, high-performing and innovative software across embedded, mobile and desktop platforms for projects using C++, Qt, QML/Qt Quick, OpenGL, Qt 3D and more. www.kdab.com/software-services
    KDAB experts regularly take time out to deliver KDAB’s world class training, in-house or at open enrolment courses around the world. We are the market leaders for training in Qt, OpenGL and C++.
    Contact us to find out more at training@kdab.com or visit our website: www.kdab.com/software-service...
    We are about 90 people, located all around the world. Our Head Office is in Sweden and we have other offices in Germany, France, the UK and the USA. www.kdab.com/about/contact/
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 6

  • @fazzah777
    @fazzah777 ปีที่แล้ว

    I love this content so much! I can understand your examples with no issue. Looking forward to next installments.
    Also your way of explaining concepts reminds me very much of @ArjanCodes, which is a great Python tutor!

  • @chouaibalihaimoud8305
    @chouaibalihaimoud8305 2 ปีที่แล้ว

    thanks for the content. Is it possible to write a a delegate in C++ and use it in QML ? for example can we reimplement a "QSqlRelationalDelegate"-like delegate that can be used from QML ?
    thanks

    • @KDABtv
      @KDABtv  ปีที่แล้ว +2

      Hi!
      You could indeed write a QQuickItem-derived delegate class in C++ and use that in QML, for custom rendering. But for custom editing like QSqlRelationalDelegate does, you probably want to handle this in QML. You would simply provide a QSqlQueryModel from C++ to do the database query (for a single column), and then use that in a QML combobox.
      We hope this answers your question.

    • @blackiedk
      @blackiedk ปีที่แล้ว +1

      Be aware that the delegates I'm talking about here is for the *widgets* QListView, QTableView, QTreeView, and not for their QML counterparts

  •  ปีที่แล้ว

    In step 3 through 6 the MoneyDelegate is not missing the Q_OBJECT macro ?

    • @blackiedk
      @blackiedk ปีที่แล้ว

      Indeed in real code I would add Q_OBJECT there too (as clazy also tells me I should), I guess I simply forgot.
      Technically, it is not strictly needed, as I have no signals or old style privat|protected|public slots in this class.
      Still I use it in all my classes to help debugging with Gammaray, where the presence of Q_OBJECT ensures that I see the class name in Gammaray
      Cheers
      Jesper.