Solved Display xml on qlabel
-
Hello,
Im trying to display coordinates file into a qlabel from xml file :
$MOL 6 6 0 0 0 0 0 0 0 0999 V2000 -0.7145 0.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -0.7145 -0.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 0.0000 -0.8250 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 0.7145 -0.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 0.7145 0.4125 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 0.0000 0.8250 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 1 2 2 0 0 2 3 1 0 0 3 4 2 0 0 4 5 1 0 0 5 6 2 0 0 6 1 1 0 0 M END
From this I want to see a molecule, could someone give me an idea about how to do this from file into a qlabel??
Thank you very much
-
Hi,
I don't want to nitpick but this is not XML at all.
Do you mean, you want to draw some sort of molecule based on this content ?
-
Yes, thats exactly what I wish, to display it on a qlabel, is it possible??
Thank you for your help
-
You'll have to start by painting on a QPixmap first.
-
Hi
Here is a sample of drawing on pixmap and show in a label.int GetBarHeight(int MAX) { return rand() % (MAX-5)+5; } void MainWindow::on_pushButton_released() { int h = ui->label->height(); int w = ui->label->width(); QPixmap pix(w, h); QPainter paint(&pix); pix.fill( Qt::white ); paint.setPen(QColor(0, 0, 0, 255)); int y = 0; int x = 0; int bw = 10; // bar width for (int barcount = 0; barcount < 12; ++barcount) { paint.setBrush(QColor(255-x, 34+x, 255, 255)); paint.drawRect(x, h - GetBarHeight(h), bw, h ); x += bw + 4; } paint.end(); ui->label->setPixmap(pix); }
However, to draw a molecule, you have to read that format correctly. Is it your own format or
something you found/got from another source?
It sounds a bit like https://fileinfo.com/extension/mol
which is used to describe something like
It comes in various version and contains heaps of information.
What exactly do you want to draw ? -
Thats exactly what I want, but Im not sure about how to display it, creating a qpixmap object from that file?
I want to paste the code and the program to display the molecule automatically.
So grateful for your help
-
@jss193
Hi
Well first step is to understand the format
Have you worked with this before?
-
Hi
Just as update if someone finds this later.Poster found a library http://openbabel.org/wiki/Main_Page
that can read this format and it turned out it can also export it as SVG image format.QString smiles = ui->textEdit->toPlainText(); // this is input string smi1 = smiles.toUtf8().constData(); OpenBabel::OBConversion conv; conv.SetInAndOutFormats("smi","svg"); OpenBabel::OBMol mol; conv.ReadString(&mol,smi1); conv.WriteFile(&mol,"/path/jsfbn.svg");
and we rendered it at the wanted size and displayed in a label.
void setSVG(QLabel *label, QString FileName, int width, int height)
{
QSvgRenderer render(FileName);
render.setViewBox(QRect(0, 0, width, height));
QPixmap pix(QSize(width, height));
pix.fill(Qt::white);
QPainter p(&pix);
render.render(&p, QRect(0, 0, width, height));
label->setPixmap(pix);
label->update();}
void MainWindow::showEvent(QShowEvent *event)
{
setSVG(ui->label, "e:/Bi0.svg", ui->label->width(), ui->label->height());
}Just asking QLabel to show it, would scale oddly. so we used QSvgRenderer directly.