Is there a way to convert QDomNode text to QString? - qt

I have a QDomDocument called doc and I need to make a QStringList of each of its top-level children. So far I have this:
QDomNodeList nodes = doc.childNodes();
for(int i = 0; i < nodes.size(); i++)
{
QDomText text = nodes.at(i).toText();
//do something here...
}
However, I cannot find any way to convert this to a QString. This is my first time ever working with The QDom* classes, so I'm pretty confused, and going by the docs there doesn't seem like much can be done with QDomText. Can anyone please offer some advice? Thanks in advance!

The parent QDomElement has a method QDomElement::text() returning the contents of the text node. Operating on the QDomElement level and using text() should be the most simple and standard way to get the element text.
Alternatively, you can call QDomCharacterData::data() (QDomCharacterData is a baseclass of QDomText) in cases where you go down to the QDomText node level.

Related

Why memcpy NOT work on this set<int> array case?

a is set< int> ARRAY, I want to copy it to b. BUT...
int main(){
set<int> a[10];
a[1].insert(99);
a[3].insert(99);
if(a[1]==a[3])cout<<"echo"<<endl;
set<int> b[10];
memcpy(b,a,sizeof(a));
if(b[1]==b[3])cout<<"echo"<<endl;// latch up here, what happen?
return 0;}
Do you know What is computer doing?
I assume the 'set' class you are using is a std::set? What makes you think that simplying memcpying the raw bytes of a std::set (or array of them, in this case) will work properly? This is highly dependent on the internal structure and implementation of the set class, and trying to do such a thing with anything more complicated than a primitive or array of primitives is almost guaranteed to give unexpected results. Doing this sort of raw byte manipulation when classes are involved is rarely going to be correct.
To do this properly you should iterate over the sets and use their '=' operator to assign them, which knows how to copy the contents properly:
for(int i = 0; i < 10; ++i) {
b[i] = a[i];
}
Even better you can use std::copy:
std::copy(std::begin(a), std::end(a), std::begin(b));

QTreeview iterate through list

I need to iterate through the QTreeview list with buttons the way it natively does via the keyboard arrow keys. I can get it to jump to the last item with this code, but it won't iterate through the list, it just jumps to the last item.
for( int i = 0; i < ui->TList_Tree->topLevelItemCount(); ++i )
{
ui->TList_Tree->setCurrentIndex(ui->TList_Tree->currentIndex().sibling(i,0));
}
I'm sure I'm missing something simple here.
My comments above missed the rather obvious QTreeView::indexAbove and QTreeView::indexBelow. So your button that that moves the cursor down should connect to code that does something along the lines of...
QModelIndex index = ui->TList_Tree->indexBelow(ui->TList_Tree->currentIndex());
if (index.isValid())
ui->TList_Tree->setCurrentIndex(index);
Did a quick check and this appears to do what you want.

Implementing updatePaintNode causes other QML elements to also draw incorrectly

I have discovered that geometry->setLineWidth(3); in the code below extends to other QML elements and can distort them, even if those other QML elements are "normal" QML elements (with no QQuickItem subclass beneath them). This seems odd to me that you could affect other elements and I wonder if it is a bug? The documentation says that this function should only affect the current element, but that is not my experience. Can anyone weigh in on why geometry->setLineWidth(3); has such unwieldy power?
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data){
QSGGeometry *geometry =
new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), 2);
geometry->setDrawingMode(GL_LINES);
geometry->setLineWidth(3);
geometry->vertexDataAsPoint2D()[0].set(0, 0);
geometry->vertexDataAsPoint2D()[1].set(width(), height());
QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
material->setColor(QColor(255, 0, 0));
QSGGeometryNode *node = new QSGGeometryNode;
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
delete oldNode;
return node;
}
If I omit the line geometry->setLineWidth(3); then the problem goes away.
I think it's a bug, sort of. The implementation in gsgbatchrenderer.cpp looks like this:
if (g->drawingMode() == GL_LINE_STRIP || g->drawingMode() == GL_LINE_LOOP || g->drawingMode() == GL_LINES)
glLineWidth(g->lineWidth());
The gl functions are global on the current OpenGL context so they apply to all drawing commands, and there's nothing that sets glLineWidth back to its previous value. However, any other nodes that also are in GL_LINES mode will also set the line width when it's their turn to render. So I'm guessing the other QML elements must be rendering lines without going through this code and setting the glLineWidth themselves. If I'm right then that's the bug and those other elements need to explicitly set the width.
Can you tell us what type those other 'normal' QML elements are?

How to maintain vertical scroll position in a QTableWidget

This a very simple problem to which I can find no solution:
This is my code:
qint32 pos = ui->twShow->verticalScrollBar()->value();
ui->twShow->blockSignals(true);
//Code for updating the contents QTableWidget twShow, this is done by erasing all cells and adding them again, in case it matters.
ui->twShow->blockSignals(false);
if (pos > 0){
ui->twShow->verticalScrollBar()->setValue(pos);
}
What I want to accomplish is simply to maintain the vertical scroll position. However the setValue function ignores the value pos (I've checked by printing the value before and after the instruction and both times its cero).
I have also tried:
QScrollBar *bar = ui->twShow->verticalScrollBar();
// Same code as before
ui->twShow->setVerticalScrollBar(bar); //This line crashes de program
However the last line crashes the program (which I've checked by commenting it, and it works fine).
Any advice would be greatly appreciated...
Thank you very much
QTableWidget * tw;
int desiredRow;
// before update
desiredRow = tw->row(tw->itemAt(1,1));
...
// update code
...
tw->scrollToItem( tw->item( desiredRow, 0),
QAbstractItemView::EnsureVisible | QAbstractItemView::PositionAtTop );
QAbstractItemView::EnsureVisible = 0.
The 'or' flag converts the result to an integer which is not allowed as parameter of the scrollToItem method. On the other hand enums are not intended to be used as combined flags.

A question about the performance of htmlText in TextField

I have got a performance problem about TextField.htmlText +=msg.And
I know thatTextField.appendText(msg) works better than TextField.text +=msg.So I wonder if there's some method better than TextField.htmlText +=msg?Any help would be appreciated.
Regards
Spawn
I haven't benchmarked it, but what I normally do is this:
var str:String = "bla bla";
for(var i:int = 0; i < 10; i++){
str += " foo";
}
myTextfield.htmlText = str;
However, it's likely not that much of a boost unless you're doing large amounts of text and/or iterations.
Concatenate your text in a variable before assigning it to the htmlText property of any control. Every time you change that property you are calling all the lifecycle display methods like commitProperties, measure, and updateDisplayList, all of which take time to render.

Resources