Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView subclass - Call setRowHidden from paintEvent
Forum Updated to NodeBB v4.3 + New Features

QTableView subclass - Call setRowHidden from paintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 574 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Herman Nordberg
    wrote on last edited by
    #1

    I have written a QTableView which should be able to hide rows based on a certain condition as well as a flag, that the user can set in the handleCollapse slot.

    	void OrderBookView::paintEvent(QPaintEvent* event) {
    		for (int row = 0; row < model()->rowCount(); row++) {
    			setRowHidden(row, isCollapsed && !rowHasOrder(row));
    		}
    
    		QTableView::paintEvent(event);
    	}
    
    	void OrderBookView::handleCollapse() {
    		isCollapsed = !isCollapsed;
    	}
    
    	bool OrderBookView::rowHasOrder(size_t row) {
    		QStandardItemModel* m = dynamic_cast<QStandardItemModel*>(model());
    		if (m) {
    			return !(m->item(row, 0)->text().isEmpty() && m->item(row, 1)->text().isEmpty() 
    				&& m->item(row, 3)->text().isEmpty() && m->item(row, 4)->text().isEmpty());
    		}
    
    		return false;
    	}
    

    Is it safe to call setRowHidden from paintEvent? The reason why I call it before every repaint is, that the user can add more rows to the model which have to be hidden right away based on the rowHasOrder condition, so just hiding all currently existing rows in handleCollapse would not be enough.
    Is there maybe a better way to achieve what I am looking for?

    JonBJ 1 Reply Last reply
    0
    • H Herman Nordberg

      I have written a QTableView which should be able to hide rows based on a certain condition as well as a flag, that the user can set in the handleCollapse slot.

      	void OrderBookView::paintEvent(QPaintEvent* event) {
      		for (int row = 0; row < model()->rowCount(); row++) {
      			setRowHidden(row, isCollapsed && !rowHasOrder(row));
      		}
      
      		QTableView::paintEvent(event);
      	}
      
      	void OrderBookView::handleCollapse() {
      		isCollapsed = !isCollapsed;
      	}
      
      	bool OrderBookView::rowHasOrder(size_t row) {
      		QStandardItemModel* m = dynamic_cast<QStandardItemModel*>(model());
      		if (m) {
      			return !(m->item(row, 0)->text().isEmpty() && m->item(row, 1)->text().isEmpty() 
      				&& m->item(row, 3)->text().isEmpty() && m->item(row, 4)->text().isEmpty());
      		}
      
      		return false;
      	}
      

      Is it safe to call setRowHidden from paintEvent? The reason why I call it before every repaint is, that the user can add more rows to the model which have to be hidden right away based on the rowHasOrder condition, so just hiding all currently existing rows in handleCollapse would not be enough.
      Is there maybe a better way to achieve what I am looking for?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Herman-Nordberg
      I wouldn't, sounds dodgily-late to me, but just opinion. "the user can add more rows to the model which have to be hidden right away" --- that seems to me what you should recognize and act upon.

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        This is for sure no task which should be done inside paintEvent().

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • H Offline
          H Offline
          Herman Nordberg
          wrote on last edited by
          #4

          Ok, thank you. Is there a way for the view to detect new rows in the model? I geuss I could just send a signal from the model to a slot in the view, but I didn't want to add too many signals that tightly couple my model with the view.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Herman-Nordberg said in QTableView subclass - Call setRowHidden from paintEvent:

            I geuss I could just send a signal from the model to a slot in the view,

            Why? See https://doc.qt.io/qt-5/qabstractitemmodel.html#signals and please read https://doc.qt.io/qt-5/model-view-programming.html

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              @Herman-Nordberg said in QTableView subclass - Call setRowHidden from paintEvent:

              I geuss I could just send a signal from the model to a slot in the view,

              Why? See https://doc.qt.io/qt-5/qabstractitemmodel.html#signals and please read https://doc.qt.io/qt-5/model-view-programming.html

              H Offline
              H Offline
              Herman Nordberg
              wrote on last edited by
              #6

              @Christian-Ehrlicher Ok thank you, I already had a feeling my solution was too hacky, hence the thread. I will work on the basics a bit, my question is answered so far.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved