Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved Display xml on qlabel

    General and Desktop
    3
    8
    584
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jss193 last edited by

      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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • J
          jss193 last edited by

          Yes, thats exactly what I wish, to display it on a qlabel, is it possible??

          Thank you for your help

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            You'll have to start by painting on a QPixmap first.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 2
            • mrjj
              mrjj Lifetime Qt Champion last edited by mrjj

              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);
              }
              
              

              alt text

              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
              alt text
              It comes in various version and contains heaps of information.
              What exactly do you want to draw ?

              1 Reply Last reply Reply Quote 5
              • J
                jss193 last edited by

                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

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @jss193 last edited by

                  @jss193
                  Hi
                  Well first step is to understand the format
                  alt text

                  Have you worked with this before?

                  1 Reply Last reply Reply Quote 2
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by

                    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.

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post