View previous topic :: View next topic |
Author |
Message |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Tue Apr 20, 2010 2:34 am Post subject: [C++ GUI] Help me convert C# into C++ |
|
|
Ok so can someone tell me how to create a gui with a button.
This is kind of what I want to achieve:
Code: | using System;
using System.Windows.Forms;
namespace HelloWorldGui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello world");
}
}
} |
_________________
Intel over amd yes. |
|
Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Apr 20, 2010 4:46 am Post subject: |
|
|
This is C++ with Qt.. Should be ~okay.
helloworld.h Code: | #ifndef HELLOWORLD_H
#define HELLOWORLD_H
#include <QMainWindow>
#include <QPushButton>
#include <QMessageBox>
#include <QHBoxLayout>
#include <QWidget>
class HelloWorld : public QMainWindow
{
Q_OBJECT
public:
HelloWorld(QWidget *parent = 0, Qt::WFlags flags = 0);
~HelloWorld();
private slots:
void buttonClicked();
};
#endif // HELLOWORLD_H
|
helloworld.cpp: Code: | #include "helloworld.h"
HelloWorld::HelloWorld(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
QWidget *widget = new QWidget( this );
QHBoxLayout *layout = new QHBoxLayout( widget );
QPushButton *button = new QPushButton( tr("Click me!"), widget );
layout->addStretch();
layout->addWidget( button );
layout->addStretch();
widget->setLayout( layout );
this->setCentralWidget( widget );
this->connect( button, SIGNAL(clicked()), SLOT(buttonClicked()) );
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::buttonClicked()
{
QMessageBox mb;
mb.setText( tr("Hello world!") );
mb.exec();
} |
main.cpp: Code: | #include <QApplication>
#include "helloworld.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HelloWorld w;
w.show();
return a.exec();
} |
Sure I could make it shorter, but this one looks at least a bit better. Well, C++ doesn't provide any GUI components itself. You might want to take a look at WinAPI if you're coding for Windows.
|
|
Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Tue Apr 20, 2010 4:49 am Post subject: |
|
|
Thx for the fast reply.
_________________
Intel over amd yes. |
|
Back to top |
|
 |
|