[SOLVED] Insert row from Qtableview to another
-
I tried this for Key_p, but i need this action for other case != Key_Dow,Key_Up
this is a part of above code
@void MonTableauWidget::keyPressEvent(QKeyEvent *event)
{int LastRow = (this->rowCount() == 0) ? 0 : this->rowCount()-1 ; int currentRows = this->currentRow() ; //int row = this->selectionModel()->currentIndex().row(); QTableWidgetItem * item = new QTableWidgetItem() ; switch (event->key()) { case Qt::Key_Down : if( this->item(LastRow,0) != 0) LastRow == currentRows ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; case Qt::Key_Up : if(LastRow > 0 & this->item(LastRow,0) == 0) LastRow == currentRows ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; case Qt::Key_P : item->setIcon( *(new QIcon("imgs/Edit-icon.png"))); this->setVerticalHeaderItem(currentRows,item); QTableWidget::keyPressEvent(event); break; default: QTableWidget::keyPressEvent(event); break; }
;@
-
Now it's working
i need your advises about my implementation, (it's bad ? , it's good ?)
@void MonTableauWidget::keyPressEvent(QKeyEvent *event)
{int LastRow = (this->rowCount() == 0) ? 0 : this->rowCount()-1 ; int currentRows = this->currentRow() ; QTableWidgetItem * item = new QTableWidgetItem() ; switch (event->key()) { case Qt::Key_Down : if( this->item(LastRow,0) != 0) LastRow == currentRows ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; case Qt::Key_Up : if(LastRow > 0 && this->item(LastRow,0) == 0) LastRow == currentRows ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; default: item->setIcon( *(new QIcon("imgs/Edit-icon.png"))); this->setVerticalHeaderItem(currentRows,item); QTableWidget::keyPressEvent(event); break; }
}
@ -
[quote author="JKSH" date="1375283945"] Unfortunately, QEvent::EnterEditFocus is not available in normal Qt builds — the QT_KEYPAD_NAVIGATION macro is not enabled.[/quote]
Right ! I forgot about that one
Quick code review (not talking about whether it works)
Your variables name doesn't seem to follow a coding style, use camel case everywhere will make it more readable (there are different styles but that's another story let's use this one). Also even if this is code have a good grammar/orthography:@int currentRows = this->currentRow(); @
currentRows implies that there are several rows implicated which is not the case following your statement.
@int lastRow = qMax(0, this->rowCount() -1);@
You should also refactor your tests, using one liner like that doesn't make the code readable, especially if in the end the second choice is the same as the else code.
@
if (this->item(LastRow,0) != 0) &&
lastRow == currentRows)
this->insertRow(this->rowCount());
else
QTableWidget::keyPressEvent(event);
@If you have a test that is done repeatedly you can also use a variable with a meaningful name
@bool isCurrentRowLast = (lastRow == currentRow);@
Don't forget that the code you are writing now might be the same code that you'll have to read in a year. Being able to go through it without having to do deep analyzes to understand what is does will prove valuable also for your co-workers.
-
Her is my code , after your advices
@void MonTableauWidget::keyPressEvent(QKeyEvent *event)
{int LastRow =qMax(0,this->rowCount()-1); int CurrentRow = this->currentRow() ; bool isCurrentRowLast = (LastRow == CurrentRow); bool isCurrentCellEmpty = this->item(LastRow,1) == 0; QTableWidgetItem * item = new QTableWidgetItem() ; switch (event->key()) { case Qt::Key_Down : if( !isCurrentCellEmpty) (isCurrentRowLast) ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; case Qt::Key_Up : if(LastRow > 0 && isCurrentCellEmpty) isCurrentRowLast ? this->removeRow(LastRow) : QTableWidget::keyPressEvent(event) ; else QTableWidget::keyPressEvent(event); break; default: item->setIcon( *(new QIcon("imgs/Edit-icon.png"))); this->setVerticalHeaderItem(CurrentRow,item); QTableWidget::keyPressEvent(event); break; }
}@
-
-
This is a memory leak:
@
item->setIcon( *(new QIcon("imgs/Edit-icon.png")));
@
setIcon() will make a copy of your icon, but the original stays in memory forever. -
Your variable names have mixed style -- some start with upper-case (e.g. CurrentRow), but some start with lower-case (e.g. isCurrentRowLast). Choose one style and don't mix them.
In Qt's style, all function names and variable names start with lower-case, and all class names start with upper-case.
- Your checking is complicated:
@
if( !isCurrentCellEmpty)
(isCurrentRowLast) ? this->insertRow(this->rowCount()) : QTableWidget::keyPressEvent(event) ;
else
QTableWidget::keyPressEvent(event);
@
Just write if (!isCurrentCellEmpty && isCurrentRowLast). Your other case has the same issue.
-
-
Ok
this my final version (Thank's for your advices )
@void MonTableauWidget::keyPressEvent(QKeyEvent *event)
{int LastRow =qMax(0,this->rowCount()-1); int CurrentRow = this->currentRow() ; bool IsCurrentRowLast = (LastRow == CurrentRow); bool IsCurrentCellEmpty = this->item(LastRow,1) == 0; QTableWidgetItem * item = new QTableWidgetItem() ; QIcon *ShowEditSignIcon = new QIcon("imgs/Edit-icon.png"); switch (event->key()) { case Qt::Key_Down : if(IsCurrentRowLast && !IsCurrentCellEmpty) this->insertRow(this->rowCount()) ; else QTableWidget::keyPressEvent(event); break; case Qt::Key_Up : if(LastRow > 0 && IsCurrentCellEmpty && IsCurrentRowLast) this->removeRow(LastRow) ; else QTableWidget::keyPressEvent(event); break; default: item->setIcon(*ShowEditSignIcon); delete ShowEditSignIcon; this->setVerticalHeaderItem(CurrentRow,item); QTableWidget::keyPressEvent(event); break; }
}
@ -
That looks a lot tidier, well done :)
You don't have to create a QIcon on the heap with new QIcon(). Just create it on the stack (no pointer) with QIcon ShowEditSignIcon("imgs/Edit-icon.png"); -- then you don't have to delete it yourself.
-
Thank's JKSH and SGaist, now i can be said, i have figured out more than 30% of Model/view architecture in Qt
if i have another question, i will post her , i will create another thread ?
-
You're welcome. Good luck with Qt programming. :)
You should start new threads for new topics. Remember to provide details of what you already tried.
-
i start use the same strategy as we did with Qtablewidget but
there is no setVerticalHeaderItem(row,item) function like Qtablewidget(it seems that QtableWidget is very easy than Qtableview
my question her :
should i sublcass QHeaderView class and implement neccessary function
after setting up vertical header @setVerticalHeader(QHeaderView * header)@or i can use another solution with out subclassing QheaderView ???
-
i have used this but no thing happened
i use Qtableview with Qsqlquerymodel
her is my code
@ui->tableView->verticalHeader()->model()->
setData(ui->tableView->verticalHeader()->model()->index(1,1),"**",Qt::DisplayRole) ;
@ -
[quote author="advseo32" date="1375355486"]if i have another question, i will post her , i will create another thread ?[/quote]Why did you ask this question and then ignore my answer?
-
i have posted this question because is similaire topic but now is Qtableview
Sorry, if i'm wrong , i will post another thread :)
-
[quote author="advseo32" date="1375434213"]i have posted this question because is similaire topic but now is Qtableview
Sorry, if i'm wrong , i will post another thread :)[/quote]Yes, please post in a new thread. It is NOT similar.
Each thread is supposed to have 1 topic, but you already have 5 topics:
How to automatically insert a row at the end when the user clicks/presses a key there
How to check if a cell is empty
How to add custom symbols to the vertical header
How to check if the user selects a different row
How to check if the user has started typing into a row
(Your new topic) How to modify headers
The title of this thread is "Insert row from Qtableview to another", but topics 2-6 are not related to this.
Forums let users search old topics to find solutions solutions. But, if a thread has many topics that are not related to the title, it's hard for someone to search.