[Solved]Put data from .txt file in QPainter
-
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();}
}@ -
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)
}
@ -
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();}
}
@
-
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();
}
@ -
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.