Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Put data from .txt file in QPainter

[Solved]Put data from .txt file in QPainter

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.6k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    BobMarly
    wrote on last edited by
    #1

    I am very new at Qt and am trying to make drawings with coordinates from different .txt files. The code below is what i could derive from the Qt documentation, however i think it only loads a file, i dont know how to get the values from the file to use in my painting. The files always have 2 colums, with x coordinates in the first colomn and y coordinates in the second.
    @static void process_line(const QByteArray &)
    {
    }

    static void process_line(const QString &)
    {
    }
    int main(){
    QFile file("a18.txt");

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return 0;

    QTextStream in(&file);
    QString line = in.readLine();
    while (!line.isNull()) {
    process_line(line);
    line = in.readLine();

    }
    }@

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      assuming your coordinates are separated with "|" character (analog for spcae-character for example).
      @
      process_line(const QString & line)
      {
      QStringList coordinates = line.split("|", QString::SkipEmptyParts );
      if( coordinates.count() == 2 )
      {
      bool checkX;
      int x = coordinates.at(0).trimmed().toInt( &checkX );
      bool checkY;
      int y = coordinates.at(1).trimmed().toInt( &checkY );

        if( checkX && checkY )
               //do something with QPoint(x,y)
      

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BobMarly
        wrote on last edited by
        #3

        This is what i have now, but it does not give any output.. what am i doing wrong? I put your code in where i thought would fit..
        @#include "dewidget.h"

        DeWidget::DeWidget(QWidget *parent)
        : QWidget( parent)
        {

        }

        DeWidget::~DeWidget()
        {
        }

        QSize DeWidget::sizeHint() const
        {
        return QSize(840, 480);
        }

        void DeWidget::paintEvent(QPaintEvent *)
        {
        QStringList coordinates = line.split("|", QString::SkipEmptyParts );
        if( coordinates.count() == 2 )
        {
        bool checkX;
        float x = coordinates.at(0).trimmed().toFloat( &checkX );
        bool checkY;
        float y = coordinates.at(1).trimmed().toFloat( &checkY );

              if( checkX && checkY );
              setAttribute(Qt::WA_OpaquePaintEvent);
              QPainter painter(this);
              QPen linepen(Qt::red);
              linepen.setCapStyle(Qt::RoundCap);
              linepen.setWidth(10);
              painter.setRenderHint(QPainter::Antialiasing,true);
              painter.setPen(linepen);
              polyPoints << QPoint(x,y );
              painter.drawPolygon(polyPoints);
                    
        }
        

        }
        static void process_line(const QByteArray &)
        {
        }

        static void process_line(const QString &)
        {
        }
        int main(){
        QFile file("a18.txt");

        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return 0;

        QTextStream in(&file);
        QString line = in.readLine();
        while (!line.isNull()) {
        process_line(line);
        line = in.readLine();

        }
        

        }

        @

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          i doubt that this code does even compile :)

          try this approach:
          @
          DeWidget::DeWidget(QWidget *parent)
          : QWidget( parent)
          {
          //read file for drawing
          QFile file("a18.txt");

          if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          {
          QTextStream in(&file);
          QString line = in.readLine();
          while (!line.isNull())
          {
          QStringList coordinates = line.split("|", QString::SkipEmptyParts );
          if( coordinates.count() == 2 )
          {
          bool checkX;
          float x = coordinates.at(0).trimmed().toFloat( &checkX );
          bool checkY;
          float y = coordinates.at(1).trimmed().toFloat( &checkY );

                if( checkX && checkY )
                      polyPoints  << QPoint(x,y);    //polyPoints needs to be of type QPolygon/QPolygonF
          }
          
            line = in.readLine(); 
          }
          
          file.close();
          

          }
          this->update();
          }

          DeWidget::~DeWidget()
          {
          }

          QSize DeWidget::sizeHint() const
          {
          return QSize(840, 480);
          }

          void DeWidget::paintEvent(QPaintEvent * event)
          {
          QWidget::paintEvent(event);

                QPainter painter(this);
                       painter.setRenderHint(QPainter::Antialiasing,true);
                QPen linepen(Qt::red);
                       linepen.setCapStyle(Qt::RoundCap);
                       linepen.setWidth(10);
                painter.setPen(linepen);
          
                painter.drawPolygon(polyPoints);
          

          }
          int main()
          {
          QApplication a;
          DeWidget w;
          w.show();
          return a.exec();
          }
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BobMarly
            wrote on last edited by
            #5

            It still doesnt give an output. the a18.txt file i stored it in my Other Files.. could there be the error ? :$
            Thank you for your effort!

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              most probably.
              Right now your code "looks ":http://qt-project.org/doc/qt-4.8/qfile.html#setFileName in the current working directory for the file to be present. Which is probably where your application binary is located.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved