Anfängerfrage: Eigene Klassen für Designer mit Visual C++
Verfasst: 4. März 2005 14:13
von Notwist
Hallo,
habe mal wieder eine Anfängerfrage. Habe das Tutorial aus der Designer-Hilfe (MetricConverter) versucht. Aber bei der Implementierung des C++-Codes mit Visual C++ scheitert es. Irgendwie passiert beim Drücken von CALCULATE nichts, obwohl ich eine Header-Datei mit neuer Klasse eingefügt habe und auch in der Main einen neuen Typ der Klasse anlegt habe. Ich muss zugeben, es fehlt noch etwas das Verständnis. Vielleicht könnt Ihr mir ja das generelle Vorgehen erklären.
Noch eine Frage: Wenn ich jetzt Variblen überprüfen will, zB ob die Werte stimmen, wie kann ich die in einer WindowsApplikation denn ausgeben bzw. das kontrollieren? Über Dos war es ja nie das Problem. Aber in einer GUI?
!!!Der Code ist angehängt!!!! (Ging leider nicht, obwohl RAR und sehr klein). Poste also dann mal den Reintext da drunter.
MAIN
Verfasst: 4. März 2005 14:13
von Notwist
#include <qapplication.h>
#include "metrickonvertermainwindow.h"
#include "metrickonvertermainwindowimpl.h"
int main( int argc, char** argv )
{
QApplication app( argc, argv );
MetricKonverterMainWindowImpl convert;
MetricKonverterMainWindow mainwindow;
app.setMainWidget(&mainwindow);
mainwindow.show();
return app.exec();
}
metrickonvertermainwindowimpl.cpp
Verfasst: 4. März 2005 14:14
von Notwist
#include "metrickonvertermainwindowimpl.h"
#include "metrickonvertermainwindow.h"
#include <qlineedit.h>
#include <qcombobox.h>
#include <qspinbox.h>
#include <qvalidator.h>
//MetricKonverterMainWindowImpl::MetricKonverterMainWindowImpl( QWidget* parent, const char* name, WFlags f )
// : MetricKonverterMainWindow( parent, name, f )
void MetricKonverterMainWindowImpl::convert(void)
{
enum MetricUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
};
enum OldUnits {
Miles,
Yards,
Feet,
Inches
};
// Retrieve the input
double input = numberLineEdit->text().toDouble();
double scaledInput = input;
setCaption("MetricKonverter");
// internally convert the input to millimeters
switch ( fromComboBox->currentItem() )
{
case Kilometers:
scaledInput *= 1000000;
break;
case Meters:
scaledInput *= 1000;
break;
case Centimeters:
scaledInput *= 10;
break;
}
//convert to inches
double result = scaledInput * 0.0393701;
switch ( toComboBox->currentItem() )
{
case Miles:
result /= 63360;
break;
case Yards:
result /= 36;
break;
case Feet:
result /= 12;
break;
}
// set the result
int decimals = decimalsSpinBox->value();
resultLineEdit->setText( QString::number( result, 'f', decimals ) );
numberLineEdit->setText( QString::number( input, 'f', decimals ) );
}
/*void MetricKonverterMainWindowImpl::valid()
{
numberLineEdit->setValidator( new QDoubleValidator( numberLineEdit ) );
numberLineEdit->setText( "10" );
numberLineEdit->selectAll();
}*/
MetricKonverterMainWindowImpl.h
Verfasst: 4. März 2005 14:15
von Notwist
#ifndef METRICKONVERTERMAINWINDOWIMPL_H
#define METRICKONVERTERMAINWINDOWIMPL_H
#include "MetricKonverterMainWindow.h"
#include "metrickonvertermainwindowimpl.h"
class MetricKonverterMainWindowImpl : public MetricKonverterMainWindow
{
Q_OBJECT
public:
MetricKonverterMainWindowImpl( QWidget* parent = 0, const char* name = 0, WFlags f = WType_TopLevel ):
MetricKonverterMainWindow( parent, name, f )
{}
public slots:
virtual void convert(void);
};
#endif
MetricKonverterMainWindow.cpp
Verfasst: 4. März 2005 14:17
von Notwist
#include "metrickonvertermainwindow.h"
#include <qvariant.h>
#include <qpushbutton.h>
#include <qcombobox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qspinbox.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qaction.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qtoolbar.h>
/*
* Constructs a MetricKonverterMainWindow as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*
*/
MetricKonverterMainWindow::MetricKonverterMainWindow( QWidget* parent, const char* name, WFlags fl )
: QMainWindow( parent, name, fl )
{
(void)statusBar();
if ( !name )
setName( "MetricKonverterMainWindow" );
setCentralWidget( new QWidget( this, "qt_central_widget" ) );
MetricKonverterMainWindowLayout = new QVBoxLayout( centralWidget(), 11, 6, "MetricKonverterMainWindowLayout");
layout68 = new QGridLayout( 0, 1, 1, 0, 6, "layout68");
toComboBox = new QComboBox( FALSE, centralWidget(), "toComboBox" );
layout68->addWidget( toComboBox, 2, 1 );
textLabel5 = new QLabel( centralWidget(), "textLabel5" );
layout68->addWidget( textLabel5, 4, 0 );
textLabel4 = new QLabel( centralWidget(), "textLabel4" );
layout68->addWidget( textLabel4, 3, 0 );
textLabel3 = new QLabel( centralWidget(), "textLabel3" );
layout68->addWidget( textLabel3, 2, 0 );
fromComboBox = new QComboBox( FALSE, centralWidget(), "fromComboBox" );
layout68->addWidget( fromComboBox, 1, 1 );
textLabel2 = new QLabel( centralWidget(), "textLabel2" );
layout68->addWidget( textLabel2, 1, 0 );
numberLineEdit = new QLineEdit( centralWidget(), "numberLineEdit" );
numberLineEdit->setFrameShape( QLineEdit::LineEditPanel );
numberLineEdit->setFrameShadow( QLineEdit::Sunken );
numberLineEdit->setAlignment( int( QLineEdit::AlignRight ) );
layout68->addWidget( numberLineEdit, 0, 1 );
layout67 = new QHBoxLayout( 0, 0, 6, "layout67");
spacer19 = new QSpacerItem( 80, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
layout67->addItem( spacer19 );
decimalsSpinBox = new QSpinBox( centralWidget(), "decimalsSpinBox" );
decimalsSpinBox->setMaxValue( 6 );
decimalsSpinBox->setValue( 3 );
layout67->addWidget( decimalsSpinBox );
layout68->addLayout( layout67, 4, 1 );
textLabel1 = new QLabel( centralWidget(), "textLabel1" );
layout68->addWidget( textLabel1, 0, 0 );
resultLineEdit = new QLineEdit( centralWidget(), "resultLineEdit" );
resultLineEdit->setPaletteBackgroundColor( QColor( 255, 252, 239 ) );
resultLineEdit->setAlignment( int( QLineEdit::AlignRight ) );
resultLineEdit->setReadOnly( TRUE );
layout68->addWidget( resultLineEdit, 3, 1 );
MetricKonverterMainWindowLayout->addLayout( layout68 );
spacer24 = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding );
MetricKonverterMainWindowLayout->addItem( spacer24 );
layout69 = new QHBoxLayout( 0, 0, 6, "layout69");
clearPushButton = new QPushButton( centralWidget(), "clearPushButton" );
layout69->addWidget( clearPushButton );
calculatePushButton = new QPushButton( centralWidget(), "calculatePushButton" );
calculatePushButton->setDefault( TRUE );
layout69->addWidget( calculatePushButton );
spacer22 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
layout69->addItem( spacer22 );
quitpushButton = new QPushButton( centralWidget(), "quitpushButton" );
layout69->addWidget( quitpushButton );
MetricKonverterMainWindowLayout->addLayout( layout69 );
// toolbars
languageChange();
resize( QSize(304, 251).expandedTo(minimumSizeHint()) );
clearWState( WState_Polished );
// signals and slots connections
connect( clearPushButton, SIGNAL( clicked() ), numberLineEdit, SLOT( clear() ) );
connect( clearPushButton, SIGNAL( clicked() ), resultLineEdit, SLOT( clear() ) );
connect( clearPushButton, SIGNAL( clicked() ), numberLineEdit, SLOT( setFocus() ) );
connect( quitpushButton, SIGNAL( clicked() ), this, SLOT( close() ) );
connect( calculatePushButton, SIGNAL( clicked() ), this, SLOT( convert() ) );
connect( decimalsSpinBox, SIGNAL( valueChanged(int) ), this, SLOT( convert() ) );
connect( fromComboBox, SIGNAL( activated(int) ), this, SLOT( convert() ) );
connect( toComboBox, SIGNAL( activated(int) ), this, SLOT( convert() ) );
// tab order
setTabOrder( numberLineEdit, fromComboBox );
setTabOrder( fromComboBox, toComboBox );
setTabOrder( toComboBox, resultLineEdit );
setTabOrder( resultLineEdit, decimalsSpinBox );
setTabOrder( decimalsSpinBox, clearPushButton );
setTabOrder( clearPushButton, calculatePushButton );
setTabOrder( calculatePushButton, quitpushButton );
// buddies
textLabel5->setBuddy( decimalsSpinBox );
textLabel4->setBuddy( resultLineEdit );
textLabel3->setBuddy( toComboBox );
textLabel2->setBuddy( fromComboBox );
}
/*
* Destroys the object and frees any allocated resources
*/
MetricKonverterMainWindow::~MetricKonverterMainWindow()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void MetricKonverterMainWindow::languageChange()
{
setCaption( tr( "MetricKonverter" ) );
toComboBox->clear();
toComboBox->insertItem( tr( "Yards" ) );
toComboBox->insertItem( trUtf8( "\x46\x75\xc3\x9f" ) );
toComboBox->insertItem( tr( "Inches" ) );
textLabel5->setText( tr( "&Decimals" ) );
textLabel4->setText( tr( "Result" ) );
textLabel3->setText( tr( "Convert &To" ) );
fromComboBox->clear();
fromComboBox->insertItem( tr( "Kilometer" ) );
fromComboBox->insertItem( tr( "Zentimeter" ) );
fromComboBox->insertItem( tr( "Millimeter" ) );
textLabel2->setText( tr( "Convert &From" ) );
numberLineEdit->setText( QString::null );
textLabel1->setText( tr( "Enter &Number" ) );
clearPushButton->setText( tr( "&Clear" ) );
clearPushButton->setAccel( QKeySequence( tr( "Alt+C" ) ) );
calculatePushButton->setText( tr( "Calculate" ) );
quitpushButton->setText( tr( "&Quit" ) );
quitpushButton->setAccel( QKeySequence( tr( "Alt+Q" ) ) );
}
void MetricKonverterMainWindow::convert()
{
qWarning( "MetricKonverterMainWindow::convert(): Not implemented yet" );
}