PDA

View Full Version : selected cells' table



dreamer
13th June 2008, 11:36
How can i know how many rows span my selection in a table??

Im my case, i have a qtexttable. When i select some cells, i'd like to know how many rows the selecion spans.......any idea???

wysota
16th June 2008, 07:09
Which widgets are you using? In general you can query for the item selection and you will be given all the selected indexes/items. Take a look at QItemSelection for example, there are some methods there that should work for you.

dreamer
16th June 2008, 09:12
I use a QGraphicsTextItem Object filled with a QTextTable.
I don't find any method of this class that i can use to obtain the specific behaviour.
I think that the "mergeCells" function, inside it, makes the work i'm looking for.


void QTextTable::mergeCells ( const QTextCursor & cursor )


...infact, it just uses the reference to a QTextCursor Object to find out how many rows and columns the selection spans.

patrik08
16th June 2008, 11:22
QGraphicsTextItem dont have the same qtextcursor selection as Qtextedit or QtextBrowser
on version 4.2. on later version i not know...


I rewrite the TextApi and run on this way....
http://www.qt-apps.org/content/show.php/GraphicsViewEdit+Layer?content=80234



void Layoutpainter::SetColumLarge()
{
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
QTextTableFormat tbforms = textCursor().currentTable()->format();
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
bool ok;
int LargeSet = QInputDialog::getInteger(0, tr("Set Cell Width"),
tr("Point Length:"),Get_Cell_Width(tbforms,cellcoolcursoris), 1, 2000, 1, &ok);
if (ok && LargeSet > 0) {
QVector<QTextLength> constraints = tbforms.columnWidthConstraints();
for (int i = 0; i < constraints.size(); ++i) {
if (i == cellcoolcursoris) {
constraints.replace(i,QTextLength(QTextLength::Fix edLength, LargeSet));
}
}
tbforms.setColumnWidthConstraints(constraints);
textCursor().currentTable()->setFormat(tbforms);
}
}
}


qreal Layoutpainter::Get_Cell_Width( QTextTableFormat TableFormat , int position )
{
qreal notfound = 0;
QVector<QTextLength> constraints = TableFormat.columnWidthConstraints();
for (int i = 0; i < constraints.size(); ++i) {
if (i == position) {
QTextLength langecell = constraints.value(i);
if (langecell.type() == QTextLength::FixedLength) {
return langecell.rawValue();
}

}
}
return notfound;
}

void Layoutpainter::MergeCellByCursorPosition()
{
if (textCursor().currentTable()) {
textCursor().currentTable()->mergeCells(textCursor());
}
}


void Layoutpainter::RemoveCoolByCursorPosition()
{
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
int cellrowcursoris = existingcell.row(); /* int value start from zero */
textCursor().currentTable()->removeColumns(cellcoolcursoris,1);
}
}

void Layoutpainter::RemoveRowByCursorPosition()
{
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
int cellrowcursoris = existingcell.row(); /* int value start from zero */
textCursor().currentTable()->removeRows(cellrowcursoris,1);
}
}


void Layoutpainter::AppendTableRows()
{
bool ok = false;
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
int cellrowcursoris = existingcell.row(); /* int value start from zero */
int approwtot = QInputDialog::getInteger(0, tr("Append NR. line row"),tr("Row:"), 1, 1, 100, 1, &ok);
if (ok && approwtot > 0) {
textCursor().currentTable()->insertRows(cellrowcursoris + 1,approwtot);
}
}
}

void Layoutpainter::AppendTableCools()
{
bool ok = false;
if (textCursor().currentTable()) {
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
int cellcoolcursoris = existingcell.column(); /* int value start from zero */
int cellrowcursoris = existingcell.row(); /* int value start from zero */
int appcooltot = QInputDialog::getInteger(0, tr("Table append Column"),tr("Cool:"), 1, 1, 10, 1, &ok);
if (ok && appcooltot > 0) {
textCursor().currentTable()->insertColumns(cellcoolcursoris + 1,appcooltot);
}
}
}


void Layoutpainter::SetTableCellColor()
{
if (textCursor().currentTable()) {
bool ok;
QTextTableCell existingcell = textCursor().currentTable()->cellAt(textCursor());
/* reformat this -> existingcell */
QTextCharFormat existformat = existingcell.format();
/* get color */
QColor col = QColorDialog::getRgba(textCursor().currentTable()->cellAt(textCursor()).format().background().color() .rgba(),&ok, 0);
if (!col.isValid()) {
return;
}
QBrush stylesin(col);
existformat.setBackground(stylesin);
existingcell.setFormat(existformat);
}
}

dreamer
16th June 2008, 11:28
anyone of these functions resolv my problem......

dreamer
19th June 2008, 08:59
problem resolved........here is the code!!



int a,numrows,c,d;
textCursor().selectedTableCells(&a,&numrows,&c,&d);