Brauch hilfe bei meinem Programm!!! (QPainter usw.) [gelöst]

Alles rund um die Programmierung mit Qt
Antworten
Qt-nator
Beiträge: 153
Registriert: 18. Dezember 2007 10:31

Brauch hilfe bei meinem Programm!!! (QPainter usw.) [gelöst]

Beitrag von Qt-nator »

Hi,

ich hab jetzt sowas geschrieben was mir wenn ich auf den Button_Draw klick eine Line zeichnen soll. Nur leider klappt das nicht so wie es soll. 1tens seh ich die Linie garnicht 2tens hab ich den verdacht das er sie garnicht dahin zeichnet wo sie hin soll naja.

Hier der Code hoffe jemand kann mir helfen:

MainWindow.h

Code: Alles auswählen


#ifndef MAINWINDOW_H_
#define MAINWINDOW_H_
#include "Ui_MainWindow.h"
class QTextEdit;
class QCheckBox;
class RenderArea;

class MainWindow : public QMainWindow, protected Ui_MainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0);




virtual ~MainWindow();

public slots:

	void aktion(void);
	void aktion_inc(void);
	void aktion_dec(void);
	void aktion_open(void);
	void aktion_save(void);
	void aktion_draw(void);

private:
	bool m_bFCB_Label;
	int i;
	QCheckBox *native;
	RenderArea *Widget;

public:
	QString text_i;
	QString fileopen;
	QString filesave;
	QString pfad;


    
	
	

};
#endif // end of #ifndef MAINWINDOW_H_

MainWindow.cpp

Code: Alles auswählen

#include "MainWindow.h"
#include "renderarea.h"
#include <string>
#include <QFileDialog>
#include <QStatusBar>
#include <QFile>
#include <QMessageBox>
#include <QTextEdit>
#include <QTextStream>
#include <QtGui>
#include <QImage>
#include <QLabel>
#include <QPicture>
#include <QPainter>
#include <QPaintEvent>

MainWindow::MainWindow(QWidget* parent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
: QMainWindow(parent, flags)
{
// create gui elements defined in the Ui_MainWindow class
Widget = new RenderArea;
m_bFCB_Label = true;
i = 0;
text_i = "";



setupUi(this);
connect(Button, SIGNAL(clicked()), this, SLOT(aktion()));
connect(Button_inc, SIGNAL(clicked()), this, SLOT(aktion_inc()));
connect(Button_dec, SIGNAL(clicked()), this, SLOT(aktion_dec()));
connect(action_Open, SIGNAL(triggered()), this, SLOT(aktion_open()));
connect(action_Save, SIGNAL(triggered()), this, SLOT(aktion_save()));
connect(Clear_Button, SIGNAL(clicked()), textEdit, SLOT(clear()));
connect(Draw_Button, SIGNAL(clicked()), this, SLOT(aktion_draw()));

}
MainWindow::~MainWindow()
{
// no need to delete child widgets, QT does it all for us
}

void MainWindow::aktion(void)
{
	Label->setVisible(m_bFCB_Label);
	m_bFCB_Label = !m_bFCB_Label;
	
}

void MainWindow::aktion_inc(void)
{
	i++;
	text_i = text_i.setNum(i);
	Label->setText(text_i);
}

void MainWindow::aktion_dec(void)
{
	i--;
	text_i = text_i.setNum(i);
	Label->setText(text_i);
}

void MainWindow::aktion_open()
{
	QString DateiName;
	
    QFileDialog::Options options;
	QString selectedFilter;
    QString fileName = QFileDialog::getOpenFileName(this,
                                tr("Parameter Map Open"),
                                DateiName,
                                tr("Parameter Map (*.*)"));
   
	QString text = textEdit->toPlainText();
    QFile f( fileName );
    f.open(QFile::ReadOnly) ;
	textEdit->setPlainText(f.readAll());
	
	  if (!fileName.isEmpty()) {
        QImage image(fileName);
        
		//ImageLabel->setPixmap(QPixmap::fromImage(image));
        
    }
	

		
	statusBar()->showMessage(("Open " + fileName), 2000);

}



void MainWindow::aktion_save()
{

	

    QFileDialog::Options options;
   	QString selectedFilter;
    QString fileName = QFileDialog::getSaveFileName(this,
                                tr("Parameter Map Save"),
                                "",
                                tr("Parameter Map(*.map)"));
    if (!fileName.isEmpty())
         statusBar()->showMessage(("Save " + fileName), 2000);


	QString text = textEdit->toPlainText();
    QFile f( fileName );
    if ( !f.open( QFile::WriteOnly ) ) {
    return;
    }
	
	QTextStream t( &f );
    t << text;
    f.close();
}

void MainWindow::aktion_draw(void)
{
 
	 void paintEvent(QPaintEvent * );
			
}	
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:
    enum Shape { Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc,
                 Chord, Pie, Path, Text, Pixmap };

    RenderArea(QWidget *parent = 0);

    
public slots:


protected:
    void paintEvent(QPaintEvent *event);

private:
    Shape shape;
   };

#endif
renderarea.cpp

Code: Alles auswählen

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


RenderArea::RenderArea(QWidget *parent)
    : QWidget(parent)
{
    

    setBackgroundRole(QPalette::Base);
    setAutoFillBackground(true);

}

void RenderArea::paintEvent(QPaintEvent * )
{
      static const QPoint points[4] = {
        QPoint(10, 80),
        QPoint(20, 10),
        QPoint(80, 30),
        QPoint(90, 70)
    };

	QRect rect(10, 20, 80, 60);

    QPainterPath path;
    path.moveTo(20, 80);
    path.lineTo(20, 30);
    path.cubicTo(80, 0, 50, 50, 80, 80);

	int startAngle = 30 * 16;
    int arcLength = 120 * 16;
	

    QPainter painter(this);
	QPen pen(Qt::black);
	painter.setPen(pen);
	
	   for (int x = 0; x < width(); x += 100) {
        for (int y = 0; y < height(); y += 100) {
            painter.save();
            painter.translate(x, y);
			painter.drawLine(rect.bottomLeft(), rect.topRight());
            painter.restore();
        }
    }
	
}



main.cpp

Code: Alles auswählen


// Header file to get a new instance of QApplication
#include <Qt/qapplication.h>
// Header file for MainWindow class
#include "MainWindow.h"
int main(int argc, char* argv[])
{
// create a new instance of QApplication with params argc and argv
QApplication app( argc, argv );
// create a new instance of MainWindow with no parent widget
MainWindow* mainWindow = new MainWindow(0, Qt::Window);
// The main widget is like any other, in most respects except that if it is deleted,
// the application exits.
app.setActiveWindow(mainWindow);
// resize window to 640 x 480
mainWindow->resize(640,480);
// Shows the widget and its child widgets.
mainWindow->show();
// Enters the main event loop and waits until exit() is called
// or the main widget is destroyed, and returns the value that
// was set via to exit() (which is 0 if exit() is called via quit()).
return app.exec();
}


Als oberfläche nehm ich MainWindow.ui was ich mit dem Qt Designer erstellt habe.

Enthält 5 Buttons 1 label und 1 TextEdit sowie 1 Widget

und was jetzt mein vormerkliches problem ist ist das wenn ich auf Button_Draw klicke das dann nichts in mein Widget (heist auch so) gezeichnet wird.

Danke im Vorraus
Zuletzt geändert von Qt-nator am 5. Februar 2008 09:15, insgesamt 1-mal geändert.
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

Abgesehen von der doch etwas komischen funktion aktion_draw() (was willst du damit machen???) sieht alles soweit ok aus. Was sagt die Konsole wenn du das Programm startest und auf den Button klickst (windows: CONFIG += console) ?
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Antworten