我還記得我第一次我試圖顯示新信息從控制檯程序製作GUI。第一次很難。請務必閱讀Qt提供的示例。這是實現你想要做的事情的一個起點。
http://qt-project.org/doc/qt-4.8/examples-widgets.html
http://qt-project.org/doc/qt-4.8/all-examples.html
您還可以找到內置在Qt Creator的歡迎屏幕示例和教程。
祝你好運。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QComboBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void translate();
private:
QTextEdit * lhs;
QTextEdit * rhs;
QComboBox * mode_comboBox;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout * vlayout = new QVBoxLayout;
QHBoxLayout * hlayout = new QHBoxLayout;
mode_comboBox = new QComboBox;
mode_comboBox->addItems(QStringList() << "Encrypt" << "Decrypt");
vlayout->addWidget(mode_comboBox);
lhs = new QTextEdit;
lhs->setText("Enter Text Here.");
rhs = new QTextEdit;
rhs->setText("See the output here.");
rhs->setReadOnly(true);
QObject::connect(lhs, SIGNAL(textChanged()), this, SLOT(translate()));
QObject::connect(mode_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(translate()));
hlayout->addWidget(lhs);
hlayout->addWidget(rhs);
vlayout->addLayout(hlayout);
this->setLayout(vlayout);
}
Widget::~Widget()
{
}
void Widget::translate()
{
if(mode_comboBox->currentText() == "Encrypt")
{
QString str = lhs->toPlainText();
rhs->setText(str.toUpper());
}
else
{
QString str = lhs->toPlainText();
rhs->setText(str.toLower());
}
}
的main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
在你的代碼裏,它說Widget我用我的類名替換它的權利?即使它說QWidget? – Bresser 2013-05-08 00:29:14
QWidget是不同的。它是基類,或者你的類從中派生出來的。 – phyatt 2013-05-08 00:32:55
下面是一些關於什麼類的介紹的鏈接。它們對GUI和複雜程序非常有用。 http://www.cplusplus.com/doc/tutorial/ http://www.cplusplus.com/doc/tutorial/classes/ http://www.cplusplus.com/doc/tutorial/classes2/ – phyatt 2013-05-08 00:33:19