将我的控制台应用程序转换为Qt

时间:2013-05-07 22:05:52

标签: c++ qt user-interface

好的,以便更详细(带代码)问题转到: http://ubuntuforums.org/showthread.php?t=2142977

我无法再次输入所有代码。

但是对于描述,我有一个Qt表单,它所做的是翻译一条消息,因此它有两个选项:将消息从英语翻译为乱码,从乱码翻译成英语(单选按钮选择; engLang和fakeLang)我想要我的if语句(如链接的网页所示),以检查是否检查了engLang的if条件,然后检查是否检查了fakeLang,然后根据检查的是哪一个,使用适当的代码进行翻译。

最后我的最后一个问题是如何实现这一点当我按下翻译它运行那些if语句并在第二个框中显示新消息(标记为secondMessage)

2 个答案:

答案 0 :(得分:1)

我记得我第一次尝试从控制台程序制作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();
}

答案 1 :(得分:0)

你所要求的并不是很清楚;我承认你试图把它分成上下文和问题,但文本可以使用一些校对。此外,代码可以而且应该在这里复制(您不必再次键入它,这就是Ctrl + C的用途),因为没有缩进或颜色,即使是简单的代码也很难遵循。

至于我对你的问题的理解,因为这更多是关于Qt而不是处理,你应该发布你的Qt代码。在此期间,我建议你查看QRadioButton类的文档和examples,特别是isChecked()。如果要在按下按钮后运行处理,则只需将()按下的事件()连接到将执行处理的自定义类的插槽中。

相关问题