QTreeView, how to call an action when mouse hovers over a row?












0















I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but that just seems to change the color of the row. What I'm looking for is when the mouse hovers over a row, a function is called which I can then use to manipulate my game.










share|improve this question























  • Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

    – eyllanesc
    Nov 23 '18 at 20:43











  • I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

    – Ender
    Nov 23 '18 at 22:18






  • 1





    you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

    – eyllanesc
    Nov 23 '18 at 22:22











  • I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

    – Scheff
    Nov 24 '18 at 11:03
















0















I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but that just seems to change the color of the row. What I'm looking for is when the mouse hovers over a row, a function is called which I can then use to manipulate my game.










share|improve this question























  • Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

    – eyllanesc
    Nov 23 '18 at 20:43











  • I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

    – Ender
    Nov 23 '18 at 22:18






  • 1





    you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

    – eyllanesc
    Nov 23 '18 at 22:22











  • I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

    – Scheff
    Nov 24 '18 at 11:03














0












0








0








I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but that just seems to change the color of the row. What I'm looking for is when the mouse hovers over a row, a function is called which I can then use to manipulate my game.










share|improve this question














I am using C++ Qt5. Currently I have a QStandardItemModel being displayed as a QTreeView with multiple rows and columns. I am aware of using setStyleSheet(), but that just seems to change the color of the row. What I'm looking for is when the mouse hovers over a row, a function is called which I can then use to manipulate my game.







qt qt5 qtreeview qstandarditemmodel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 19:54









EnderEnder

62811230




62811230













  • Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

    – eyllanesc
    Nov 23 '18 at 20:43











  • I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

    – Ender
    Nov 23 '18 at 22:18






  • 1





    you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

    – eyllanesc
    Nov 23 '18 at 22:22











  • I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

    – Scheff
    Nov 24 '18 at 11:03



















  • Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

    – eyllanesc
    Nov 23 '18 at 20:43











  • I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

    – Ender
    Nov 23 '18 at 22:18






  • 1





    you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

    – eyllanesc
    Nov 23 '18 at 22:22











  • I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

    – Scheff
    Nov 24 '18 at 11:03

















Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

– eyllanesc
Nov 23 '18 at 20:43





Could you explain me better, what data do you need from the row or do you only want that function to be called when you change rows?

– eyllanesc
Nov 23 '18 at 20:43













I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

– Ender
Nov 23 '18 at 22:18





I'm looking for a unique id from each row/col and as I hover over the row/col, a function is called with the unique id from the row & col.

– Ender
Nov 23 '18 at 22:18




1




1





you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

– eyllanesc
Nov 23 '18 at 22:22





you could give more detail about the ID besides providing a Minimal, Complete, and Verifiable example, to track the hover you could use the mouseMoveEvent method of QTreeView or the editorEvent on a delegate. Your current question is partly clear, but partly misleading, so we need more details.

– eyllanesc
Nov 23 '18 at 22:22













I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

– Scheff
Nov 24 '18 at 11:03





I agree with @eyllanesc, I would overload mouseMoveEvent(). May be, it's worth to mention QTreeView::indexAt() for this which Returns the model index of the item at the viewport coordinates point.

– Scheff
Nov 24 '18 at 11:03












1 Answer
1






active

oldest

votes


















2














You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row.
You should override paint method.



void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
painter->fillRect(option.rect, Qt::blue);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452304%2fqtreeview-how-to-call-an-action-when-mouse-hovers-over-a-row%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row.
    You should override paint method.



    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
    painter->fillRect(option.rect, Qt::blue);
    } else {
    QStyledItemDelegate::paint(painter, option, index);
    }
    }





    share|improve this answer




























      2














      You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row.
      You should override paint method.



      void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
      if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
      painter->fillRect(option.rect, Qt::blue);
      } else {
      QStyledItemDelegate::paint(painter, option, index);
      }
      }





      share|improve this answer


























        2












        2








        2







        You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row.
        You should override paint method.



        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
        painter->fillRect(option.rect, Qt::blue);
        } else {
        QStyledItemDelegate::paint(painter, option, index);
        }
        }





        share|improve this answer













        You can use delegate (http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html) and QStyle::State_MouseOver to check if mouse over a row.
        You should override paint method.



        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        if(index.row() == 2 && (option.state & QStyle::State_MouseOver)) {
        painter->fillRect(option.rect, Qt::blue);
        } else {
        QStyledItemDelegate::paint(painter, option, index);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 12:09









        Andrey SemenovAndrey Semenov

        576410




        576410
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452304%2fqtreeview-how-to-call-an-action-when-mouse-hovers-over-a-row%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python