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.
{
}
void GridDelegate
::drawFocus(QPainter* painter,
{
if (option.
state & QStyle::State_HasFocus)
{
pen.setWidth(3);
painter->setPen(pen);
painter->drawRect(rect);
}
else if (option.
state & QStyle::State_Selected) {
pen.setWidth(3);
painter->setPen(pen);
painter->drawRect(rect);
}
}
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);
}
}
To copy to clipboard, switch view to plain text mode
in the table i use as:
{
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
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()
{
{
strPrint+="\t";
str+=strPrint;
i->setText(" ");
}
}
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(" ");
}
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks