PDA

View Full Version : Problem in Cut Operation



ankurjain
12th April 2006, 13:55
Hi,
i made a dll that provides a table in the main project and some basic operations that can be done on table. Compare it with the table of excel, it provides the functions of cut,copy,paste etc on context menu.

The requirement was to make the grid ( table) behaviour more similar to excel. For this the selection of the cells in the table also need to be changed. So i also used delegates here.

i am having problem in cut operations.
First this is the delegate to use for table border thickening.



GridDelegate::GridDelegate(QObject *parent) : QItemDelegate(parent)
{

}

void GridDelegate::drawFocus(QPainter* painter,
const QStyleOptionViewItem &option,
const QRect &rect) const

{
if (option.state & QStyle::State_HasFocus)

{

QPen pen(Qt::black);

pen.setWidth(3);

painter->setPen(pen);

painter->drawRect(rect);

}
else if (option.state & QStyle::State_Selected)
{
QPen pen(Qt::black);

pen.setWidth(3);

painter->setPen(pen);

painter->drawRect(rect);
}
}


in the table i use as:



void Eventtable::tableItemDblClicked(QTableWidgetItem *item)
{
QTableWidget *tt = item->tableWidget();

int nRow = tt->row(item);
int nColumn = tt->column(item);

switch (nColumn)
{

case 1:
eventtable_data->setItemDelegate(new SpinBoxDelegate(this));
break;
.........................
default:
if(tt == eventtable_data)
tt->setItemDelegate(new GridDelegate(this));
else
if(tt == eventtable_msg)
if(nColumn >= 1)
{
tt->setItemDelegate(new EditDelegate(this));
}
else
tt->setItemDelegate(new GridDelegate(this));
break;
}
}

this way i am able to get the delegates on dblclik of the cell.

Now if i cut the item in cell, and again select cell the border was not showing around the cell, even though it was selected.

to get around the problem i set the text in the cell as " " instead of "" in the cut function.
cut looks like this :


void QGrid::cut()
{
QString str,str1;
foreach (QTableWidgetItem *i,this->selectedItems())
{
QString strPrint(i->text());
strPrint+="\t";
str+=strPrint;
clip->setText(str,QClipboard::Clipboard);
i->setText(" ");
}
}


But then again, in the first column i did a validation as per which space must not be in the first column. the very concept for cut function and showing the cell selected failed here.
if i remove space in cut, the selection border is not showing. if i keep space error due to space validation comes.

so how can i show the border across the cell ?
how can i activate the delegate on keyboard keys. for instance if my cell is selected, pressing any key (alphanumeric) must trigger the edit mode and thus the corresponding delegate for the cell. same must be the operation of F2 key.

Please help if someone has a workaround. if i am not clear in my explanation tell me.

Thanx in advance

wysota
13th April 2006, 17:55
I think you should use a model-view approach here.


this way i am able to get the delegates on dblclik of the cell.
This is not good. The delegate should be set upon creation of the view and not on double click. The double click should trigger edit mode, which in turn should ask the delegate to provide an editor for the cell. You shouldn't mess with the delegates this way.


so how can i show the border across the cell ?
In what way should it be "across"? You mean you want to have a rectangle containing many cells selected? If you use the model-view approach this should be done automatically.


how can i activate the delegate on keyboard keys. for instance if my cell is selected, pressing any key (alphanumeric) must trigger the edit mode and thus the corresponding delegate for the cell. same must be the operation of F2 key.
You don't "activate" the delegate. The view asks the delegate to do some things... You should only trigger appropriate actions on the view (like update() and edit()).

In your case I think you need to set edit triggers (QAbstractItemView::setEditTriggers()) to QAbstractItemView::DoubleClicked|QAbstractItemView ::EditKeyPressed.

ankurjain
14th April 2006, 06:24
hi wysota,
thanks for ur reply ...

but since i was put in a team in which design and work was already going on. and this table was constructed using QTableWidget instead of using Model/View Architecture.



In what way should it be "across"?


I actually want the border around the cell :) ( sry for bad english ) or if a row is selected the border ( rectangle ) around the row.



This is not good. The delegate should be set upon creation of the view and not on double click. The double click should trigger edit mode, which in turn should ask the delegate to provide an editor for the cell. You shouldn't mess with the delegates this way.


i am calling the editor in the delegate. as one requirement was like that, bigger editor window on dbl clking the cell.

Just For My Knowledge
Can i edit data also and update it in Model/View architecuture ??

Also deadline is close for 1st release, so i can't change the design right now.
So please help out ....

wysota
14th April 2006, 13:20
but since i was put in a team in which design and work was already going on. and this table was constructed using QTableWidget instead of using Model/View Architecture.
It doesn't mean the team can't change its decisions, right? You'll experience lots of problems with the itembased approach. The model-view architecture handles some aspects of the application on its own. Without it, you'll have much more coding to do.


I actually want the border around the cell :) ( sry for bad english ) or if a row is selected the border ( rectangle ) around the row.
And the problem is...? Drawing the selection is the views and delegates responsibility. You have a selection model associated with the view and you can act depending on its contents.


i am calling the editor in the delegate. as one requirement was like that, bigger editor window on dbl clking the cell.
I don't quite understand... You want to have two different kinds of editors? That surely doesn't require changing the delegate upon a click on the cell.


Can i edit data also and update it in Model/View architecuture ??
Of course you can.


Also deadline is close for 1st release, so i can't change the design right now.
It's not that much more work as you think. And you'll probably save some time later. It's better to correct misdesigns earlier, because the consequences of such misdesigned won't require as many changes to the application as when you want to correct them on a later stage.

ankurjain
14th April 2006, 13:23
ok,

right now i m working towards alpha release ( 1 day more to go, hiw can i cahnge the design rite now) , after this release i will try to change the design again and communicate to team and integrate with their code.

if some problem there i will again post here.

Thanx 4 guidance.