PDA

View Full Version : reading integers from QTextEdit doesn't go right ?



mernarezk
1st December 2013, 19:28
I have 6 push buttons, each one draws a different shape, and I have multiple textEdits that takes coordinates from user as an input, the problem is wheneve I enter the coordinates of a line and save it in a variable and call the method draw it doesnt draw anything, though I've tried the draw functions with numbers as parameters and worked just fine, what could be the problem ? I am still a newbie to QT


#include "dialog.h"
#include "ui_dialog.h"
#include<QPixmap>
#include <QLayout>
#include <QLabel>
bool p1=false;
bool p2=false;
bool p3=false;
bool p4=false;
bool p5=false;
bool p6=false;
bool ok=true;
int initX,initY,x2,y2,x3,y3,r1,r2,l,w;
QPixmap *pm;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}
void Dialog::paintEvent(QPaintEvent *e){
QDialog::paintEvent(e);
//QLabel::paintEvent(event);
QPainter painter(this);
if(p1){

painter.drawEllipse(initX,initY,r1,r1);
painter.save();
p1=false;
}
if(p2){
painter.drawEllipse(initX,initY,r1,r2);
painter.save();
p1=false;
}
if(p3){
painter.drawLine(initX,initY,x2,y2);
painter.save();
p3=false;
}
if(p4){
painter.drawRect(initX,initY,l,l);
painter.save();
p4=false;
}

if(p5){
painter.drawRect(initX,initY,l,w);
painter.save();
p5=false;
}
if(p6){
painter.drawLine(initX,initY,x2,y3);
painter.save();
painter.drawLine(x2,y2,x3,y3);
painter.save();
painter.drawLine(x3,y3,initX,initY);
painter.save();
p6=false;
}
}
void Dialog::on_pushButton_3_clicked()
{
p3=true;
update();
}



void Dialog::on_pushButton_clicked()
{
p1=true;
update();
}

void Dialog::on_pushButton_2_clicked()
{
p2=true;
update();
}

void Dialog::on_pushButton_4_clicked()
{
p4=true;
update();
}

void Dialog::on_pushButton_5_clicked()
{
p5=true;
update();
}

void Dialog::on_pushButton_6_clicked()
{
p6=true;
update();
}

void Dialog::on_textEdit_textChanged()
{
// ui->textEdit->
initX=ui->textEdit->toPlainText().toInt(&ok,10);
// initX=10;
}

void Dialog::on_textEdit_2_textChanged()
{
initY=ui->textEdit->toPlainText().toInt(&ok,10);
//initY=10;
}

void Dialog::on_textEdit_3_textChanged()
{
x2=ui->textEdit->toPlainText().toInt(&ok,10);
// x2=100;
}

void Dialog::on_textEdit_4_textChanged()
{
y2=ui->textEdit->toPlainText().toInt(&ok,10);
// y2=100;
}

void Dialog::on_textEdit_5_textChanged()
{
x3=ui->textEdit->toPlainText().toInt(&ok,10);
}

void Dialog::on_textEdit_6_textChanged()
{
y3=ui->textEdit->toPlainText().toInt(&ok,10);
}

void Dialog::on_textEdit_7_textChanged()
{
r1=ui->textEdit->toPlainText().toInt(&ok,10);
}

void Dialog::on_textEdit_8_textChanged()
{
r2=ui->textEdit->toPlainText().toInt(&ok,10);
}

void Dialog::on_textEdit_9_textChanged()
{
l=ui->textEdit->toPlainText().toInt(&ok,10);
}

void Dialog::on_textEdit_10_textChanged()
{
w=ui->textEdit->toPlainText().toInt(&ok,10);
}

ChrisW67
1st December 2013, 21:01
Have you looked at the value of ok after each conversion toInt()?
If you want numbers why not use a QLineEdit with a QIntValidator or a QSpinBox?

Other things...
QPainter::save() does not do anything useful in your code.
File scoped variables are bad form in C++.
QSignalMapper could be useful.

mernarezk
1st December 2013, 21:56
Thanks the spinner trick worked just fine :) ... the painter.save() line it holds the current shapes drawn , when I remove this line and call the draw method it erases previous shapes

ChrisW67
1st December 2013, 22:20
QPainter::save() saves the current state of the painter not the painted device. That state is stored internally to the QPainter and can only live as long as the QPainter exists, which in this case is until the end of the paintEvent(). You never call QPainter::restore() so the saved states are never reused.

Subsequent calls to the paintEvent() only draw the shape associated with the last clicked button because that is the logic you have implemented by clearing the px flag after drawing the shape x.

mernarezk
2nd December 2013, 15:43
No the Px flags are not the problem, I searched and I should use the painter on a painting device like a PixMap or anything like canvas in java but I am have no idea how to use them, so any idea how can I overcome the problem that painter deletes previous shapes when trying to draw a new one ? ...one more question is there anyway to invert my axis ? x=0 , y=0 are at the top left corner is it possible to make them like cartesian coordinates x,y=0,0 at the botton left cornor ?

ChrisW67
2nd December 2013, 23:32
If you want to accumulate a set of marks on the page then you need to keep a persistent track of either the page or the actions required to draw it. I suggest you spend some time with the Scribble Example, particularly the ScibbleArea class.

For the axis reversal:
Coordinate System: Transformations
QPainter::scale() with a negative scale in the Y direction.