Seite 1 von 1

Extra Fenster in Anwendung öffnen

Verfasst: 19. August 2010 17:52
von undertone
Hallo,

ich schreibe ein Programm bei dem durch nach einem Dialogaufruf (der funktioniert) ein extra Fenster aufgehen soll, indem ein Histogramm (mit qwt) angezeigt werden soll. Leider bleibt das Fenster immer leer. Nichtmal Buttons oder ähnliches werden angezeigt. Folglich mache ich irgendetwas falsch. Hier mal ein paar Auszüge aus meinem Programm:

Für mein Histogramm-Fenster habe ich eine extra Klasse angelegt.

HistogramWindow.h:

Code: Alles auswählen

#ifndef HISTOGRAMWINDOW_H
#define HISTOGRAMWINDOW_H
#include <QtGui>
class HistogramWindow : public QWidget
 {
     Q_OBJECT
 public:
     HistogramWindow(QWidget *parent = 0);
 private:
     QPushButton *closeButton;
 };
#endif
HistogramWindow.cpp:

Code: Alles auswählen

#include "HistogramWindow.h"
#include <qwt_plot.h>

HistogramWindow::HistogramWindow(QWidget *parent)
     : QWidget(parent)
 {
	 QwtPlot plot(parent);
	 plot.setCanvasBackground(QColor(Qt::white));
	 plot.setTitle("Histogram");

     closeButton = new QPushButton(tr("&Close"));
     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

     setWindowTitle(tr("Histogram Window"));
 }
In meinem Hauptprogramm (Abwandlung vom Qt-ImageViewer) rufe ich das Fenster dann im SLOT void ImageViewer::histogram() so auf:

Code: Alles auswählen

HistogramWindow *histogramWindow = new HistogramWindow(this);
histogramWindow->setWindowFlags(Qt::Window);
QPoint pos = histogramWindow->pos();
if (pos.x() < 0)
     pos.setX(0);
if (pos.y() < 0)
     pos.setY(0);
histogramWindow->move(pos);
histogramWindow->show();
Was mache ich falsch? Es wäre toll, wenn mir jemand helfen würde. Ich brauche allerdings eine Erklärung für Dummies :oops:
Vielen Dank schonmal!!!

Verfasst: 19. August 2010 18:16
von Christian81
C++ Grundlagen, Stichwort 'Gültigkeitsbereich von Variablen'.
Des weiteren fehlt ein Layout im HistogramWindow.

Verfasst: 19. August 2010 18:26
von undertone
Danke für die Antwort, aber damit kann ich nichts anfangen. Welche Variablen sollen denn wo nicht gültig sein? Ist ein Layout immer notwendig?

Verfasst: 19. August 2010 18:34
von Christian81
Wie lang lebt wohl das QwtPlot?

Verfasst: 19. August 2010 18:53
von undertone
So funktionierts:
HistogramWindow.h:

Code: Alles auswählen

#ifndef HISTOGRAMWINDOW_H
#define HISTOGRAMWINDOW_H

#include <QtGui>
#include <qwt_plot.h>

class HistogramWindow : public QWidget
 {
     Q_OBJECT

 public:
     HistogramWindow(QWidget *parent = 0);

 private:
     QPushButton *closeButton;
	 QwtPlot *plot;
 };

#endif
HistogramWindow.cpp:

Code: Alles auswählen

#include "HistogramWindow.h"
#include <qwt_plot_grid.h>


HistogramWindow::HistogramWindow(QWidget *parent)
     : QWidget(parent)
 {
	 plot = new QwtPlot(this);
	 plot->setCanvasBackground(QColor(Qt::white));
	 plot->setTitle("Histogram");

	 QwtPlotGrid *grid = new QwtPlotGrid;
	 grid->enableXMin(true);
	 grid->enableYMin(true);
	 grid->setMajPen(QPen(Qt::black, 0, Qt::DotLine));
	 grid->setMinPen(QPen(Qt::gray, 0, Qt::DotLine));
	 grid->attach(plot);
	 plot->show();

     closeButton = new QPushButton(tr("&Close"));
     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

	 QHBoxLayout *hlayout = new QHBoxLayout;
	 hlayout->addStretch();
	 hlayout->addWidget(closeButton);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(plot);
     layout->addLayout(hlayout);
     setLayout(layout);
	 resize(600,400);
     setWindowTitle(tr("Histogram Window"));
 }