PDA

View Full Version : QTextTable is slow!



Ishmael
25th September 2009, 19:01
I have a string that is a few thousand characters long, and I would like to put each character of that string in its own cell of a text table. I've been trying to do this using:

cursor = table->cellAt(i,j).firstCursorPosition();
cursor.insertText(letter);

but this is excruciatingly slow for anything more than maybe one hundred characters. Is there a better way of doing this?

Thanks for your help!

wysota
26th September 2009, 10:15
It's probably a bad idea to build such a table. What is the purpose of the table?

Ishmael
28th September 2009, 20:10
I am trying to plot multiple gene sequences. The letters in each sequence, e.g. AGTCNNAG need to be aligned in a specific way, which I have been able to achieve by setting the font to fixed-width, then printing strings padded with the appropriate number of spaces. The problem with this method is a) I don't have any way of accessing individual characters - I would like to be able to make a click-event callback for each letter, and b) I don't like the look of fixed-width fonts.

I've started exploring the option of using QGraphicsView to display the characters. Does this seem like a reasonable option? The documentation claims that it can handle "millions" of objects very rapidly. Hopefully that means that it will be able to display a few thousand characters to the screen instantly. There is also the possibility of using the Model-View Framework, which is also brand new to me. Would this be the best option?

Thanks for your help!

wysota
28th September 2009, 22:11
I've started exploring the option of using QGraphicsView to display the characters. Does this seem like a reasonable option?
Yes.


There is also the possibility of using the Model-View Framework, which is also brand new to me. Would this be the best option?

Model-View is fine as well but it will probably be easier for you to use Graphics View.

Ishmael
29th September 2009, 01:52
I've been trying to use QGraphicsView with limited success. Since what I would like to do involves putting characters in a table format, it seems logical to use QGraphicsGridLayout, but I'm having trouble. So far, this is what I have:

// This works
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(scene);
tabs->addTab(view, "graphics");
QGraphicsTextItem *c = new QGraphicsTextItem;
c->setPlainText("bla");
scene->addItem(c); // WORKS, BUT DOESN'T USE LAYOUT

// DOESN'T WORK - Why am I not allowed to create a new QGraphicsLayoutItem?
QGraphicsGridLayout *layout = new QGraphicsGridLayout;
QGraphicsLayoutItem *layoutItem = new QGraphicsLayoutItem;
layoutItem->setGraphicsItem(c);
layout->addItem(layoutItem, 0, 0);

// DOESN'T WORK - Why not?
layout->itemAt(0,0)->setGraphicsItem(c);

Also, I am having trouble grasping when to use the "new" keyword. My understanding is that 'new' allocates dynamic memory, whereas a declaration without 'new' allocates static memory, which is freed as soon as the function exits. Yet in the example shown here (http://doc.trolltech.com/4.5/qgraphicsgridlayout.html#details), the scene is declared WITHOUT the use of "new", but QGraphicsGridLayout is declared WITH 'new'. Why?

Am I barking up the wrong tree with the QGraphicsGridLayout?

Thanks again!

wysota
29th September 2009, 09:51
Using the layout is ok but it's not needed in your case as you won't be doing any resizing or refitting the scene.

Here is something I crafted quickly:

#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
QString text = "Example text";
QFont font;
font.setFamily("monospace");
font.setPointSize(24);
QFontMetrics fm(font);
int x = 0;
for(int i=0;i<text.size();i++){
QChar ch = text.at(i);
QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem;
item->setText(QString(ch));
item->setFont(font);
scene.addItem(item);
item->setPos(x, 0);
item->setFlag(QGraphicsItem::ItemIsSelectable);
x += fm.width(item->text())+fm.rightBearing(ch)+10; // +10 for additional spacing
}
view.show();
return app.exec();
}

You can control the distances between characters by adjusting x (and "y" if you want multiple rows).

Ishmael
29th September 2009, 21:25
Wow. What can i say? This is fantastic! Thank you!