Hilfe bei einer Funktion // Zeichnen [gelöst]

Verschiedenes zu Qt
Qt-nator
Beiträge: 153
Registriert: 18. Dezember 2007 10:31

Beitrag von Qt-nator »

SO hab mir jetzt selbst geholfem.

Ich kann jetzt einen Text eingeben in ein Text edit dann drück ich auf den Draw Button und dann hab ich das was ich geschrieben habe gezeichnet.

das geht dann so.

Eingabe:

drawText 10,10,100,100

drawButton drücken:

Zeichnung.


So aber was ich jetzt noch fragen bzw. gerne wissen wollte und hoffentlich mal eine brauchbare Lösung bekomme ist:

Da ich den Gesamten Inhalt des Textedits in eine Datei speicher und diese dann auslese... und ich das so nicht will ist die Frage.

Wie bekomme ich diesen String

QString text = smallEditor->toPlainText();

in meine reanderarea.cpp.

wie gesagt im Moment speichere ich in eine datei und die lese ich dann aus.

ich möchte aber nicht speichern.

Wenn der Code zum verständniss hilft dann poste ich hin. Müsst ihr nur sagen.
RHBaum
Beiträge: 1436
Registriert: 17. Juni 2005 09:58

Beitrag von RHBaum »

Verstehe die frage ned wirklich ....

due drueckst doch deine taste, dann bekommst nen event ? oder ?
das event haengt wo ? am mainfenster oder ?
das mainfenster kennt beides ? die renderarea und den TextEdit ?

wo ist das problem, bei dem eventhaendler den text ausm edit auszulaesen und ueber ne funktion an die renderarea zu uebergeben ???

P.S.
Dein beispiel was du versuchst da umzusetzen klingt viel zu kompliziert.
Versuch lieber erst mal paar einfachere dinge. da lassen sich fragen auch besser im forum zu stellen .... bis man ein gefuehl fuer die QT bekommt, dann kann man auch kompliziertere dinge machen.

Ciao ...
Qt-nator
Beiträge: 153
Registriert: 18. Dezember 2007 10:31

Beitrag von Qt-nator »

hmmm ich poste einfach mal den Code

main.cpp

Code: Alles auswählen

#include <QApplication>
#include "window.h"


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.show();
    return app.exec();
}
window.h

Code: Alles auswählen

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>
class QGroupBox;
class QCheckBox;
class QComboBox;
class QLabel;
class QSpinBox;
class RenderArea;
class QPushButton;
class QTextEdit;


class Window : public QWidget
{
    Q_OBJECT

public:
    Window();
	QString *text;
	
private slots:

	void DrawChangedButton();
    void Clear_Buttonfunktion();

private:
	
    RenderArea* renderArea;
    QComboBox* shapeComboBox;
    QCheckBox* antialiasingCheckBox;
	QPushButton* button;
	QPushButton* Clear_Button;
	QTextEdit* smallEditor;

};

#endif

window.cpp

Code: Alles auswählen

#include <QtGui>
#include <QString>
#include "renderarea.h"
#include "window.h"

Window::Window()
{
	Clear_Buttonfunktion();
	renderArea = new RenderArea;
	smallEditor = new QTextEdit;
    smallEditor->setPlainText(tr(""));
	shapeComboBox = new QComboBox;
	button = new QPushButton(tr("Draw"));
	Clear_Button = new QPushButton(tr("Clear"));
	connect(button, SIGNAL(clicked()), this, SLOT(DrawChangedButton()));
	connect(Clear_Button, SIGNAL(clicked()), smallEditor, SLOT(clear()));
	connect(Clear_Button, SIGNAL(clicked()), this, SLOT(Clear_Buttonfunktion()));


    QGridLayout *mainLayout = new QGridLayout;
	mainLayout->setColumnStretch(0, 1);
    mainLayout->setColumnStretch(3, 1);
    mainLayout->addWidget(renderArea, 0, 0, 3, 3);
    mainLayout->setRowMinimumHeight(1, 6);
	mainLayout->addWidget(smallEditor, 2, 2, 1, 1);
	mainLayout->addWidget(button, 3, 2);
	mainLayout->addWidget(Clear_Button, 4, 2);
    setLayout(mainLayout);
    setWindowTitle(tr("Drawing"));
}

void Window::DrawChangedButton()
{
	QString smallEditorText = smallEditor->toPlainText();
	QFile f( "Test.txt" );
    if ( !f.open( QFile::WriteOnly ) ) {
    return;
    }

	QTextStream t( &f );
    t << smallEditorText;
    f.close();
	update();

}

void Window::Clear_Buttonfunktion()
{
	QFile f( "Test.txt" );
    if ( !f.open( QFile::WriteOnly ) ) {
    return;
    }

	QTextStream t( &f );
    t << "";
    f.close();
	update();
}
renderarea.h

Code: Alles auswählen

#ifndef RENDERAREA_H
#define RENDERAREA_H

#include <QBrush>
#include <QPen>
#include <QPixmap>
#include <QWidget>

class RenderArea : public QWidget
{
	Q_OBJECT
	
public:
  
    RenderArea(QWidget *parent = 0);
	QSize minimumSizeHint() const;
    QSize sizeHint() const;
  
protected:

	void paintEvent(QPaintEvent *event);

private:
	
	
    QPen pen;
    QBrush brush;
    bool antialiased;
    QPixmap pixmap;

};

#endif
renderarea.cpp

Code: Alles auswählen

#include <QtGui>
#include "renderarea.h"
#include "window.h"


RenderArea::RenderArea(QWidget *parent): QWidget(parent)
{
	 
	setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);
	
}

QSize RenderArea::minimumSizeHint() const
{
    return QSize(100, 20);
}

QSize RenderArea::sizeHint() const
{
    return QSize(100, 20);
}

void RenderArea::paintEvent(QPaintEvent * /* event */)
{
	QPainter painter(this);
    painter.setPen(pen);
    painter.setBrush(brush);       	
	QString Text;
	QFile f( "Test.txt" );
    f.open(QFile::ReadOnly);
	Text = f.readAll();
	Text = Text.simplified();

if (Text.contains("drawText", Qt::CaseInsensitive))
{		
	QStringList list1 = Text.split(",", QString::SkipEmptyParts);
	QString xKoordinate = list1[0];
	int xKoordinatezahl = xKoordinate.toInt();
	QString yKoordinate = list1[1];
	int yKoordinateZahl = yKoordinate.toInt();
	QString drawText = list1[2];
	painter.drawText(xKoordinatezahl,yKoordinateZahl,drawText);
	update();
}

if  (Text.contains("drawLine", Qt::CaseInsensitive))
{
	QStringList list1 = Text.split(",", QString::SkipEmptyParts);
	QString xKoordinate1 = list1[0];
	int xKoordinatezahl1 = xKoordinate1.toInt();
	QString yKoordinate1 = list1[1];
	int yKoordinatezahl1 = yKoordinate1.toInt();
	QString xKoordinate2 = list1[2];
	int xKoordinatezahl2 = xKoordinate2.toInt();
	QString yKoordinate2 = list1[3];
	int yKoordinatezahl2 = yKoordinate2.toInt();
	painter.drawLine(xKoordinatezahl1,yKoordinatezahl1,xKoordinatezahl2,yKoordinatezahl2);
	update();
}

if  (Text.contains("drawEllipse", Qt::CaseInsensitive))
{
	QStringList list1 = Text.split(",", QString::SkipEmptyParts);
	QString xKoordinate1 = list1[0];
	int xKoordinatezahl1 = xKoordinate1.toDouble();
	QString yKoordinate1 = list1[1];
	int yKoordinatezahl1 = yKoordinate1.toDouble();
	QString xKoordinate2 = list1[2];
	int xKoordinatezahl2 = xKoordinate2.toDouble();
	QString yKoordinate2 = list1[3];
	int yKoordinatezahl2 = yKoordinate2.toDouble();
	QRectF rectangle(xKoordinatezahl1, yKoordinatezahl1, xKoordinatezahl2, yKoordinatezahl2);
	QPainter painter(this);
	painter.drawEllipse(rectangle);
	update();
}		
}
So Frage ist dann wie ich den Inhalt von textEdit der ja dann in das File gespeichert wird als QString bekomme den ich dann an Renderarea.cpp weitergeben kann...

das krieg ich nicht hin.
Antworten