0

我正在使用 Visual Studio (c++),并且正在使用 Qt 和 OpenCV。
我想要做的是创建一个窗口,我可以在其中输入稍后将在算法中使用的几个值。这些变量是 double 和 int 类型。
我查看了 Qt 文档和互联网,但没有找到合适的方法。我也不是在寻找一个弹出对话框并要求用户输入值,只是一个带有多个字段的窗口来输入我的值并更新它们。任何帮助将不胜感激,谢谢

编辑:我现在使用 QDoubleSpinBox 输入一个双精度值和一个按钮来更新和打印控制台中的值。我为我的按钮创建了一个类,以便能够在 main.h 中使用自定义 SLOTS:

class MyButton : public QWidget
{
    Q_OBJECT

public:
    MyButton();

public slots:
    void updateValue(QDoubleSpinBox* input);
};  

这是main.cpp:

#include "main.h"

#include <QtGui>

#include <iostream>

using namespace std;

double value;

MyButton::MyButton() : QWidget()
{
    QPushButton *update = new QPushButton("update",this);
    connect(update, SIGNAL(clicked()), this, SLOT(updateValue(QDoubleSpinBox)));
}

void MyButton::updateValue(QDoubleSpinBox *input)
{
    input->update();
    value = input->value(); 
    cout<<value;
}  

现在我不确定在'main'函数中写什么来使用它来创建按钮。到目前为止,这是我的“主要”功能:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget window;

    QDoubleSpinBox *input = new QDoubleSpinBox();
    input->setValue(5.00);

    QVBoxLayout *vbox1 = new QVBoxLayout;
    vbox1->addWidget(input);

    window.setLayout(vbox1);
    window.resize(800,600);
    window.show();
    window.setWindowTitle(QApplication::translate("toplevel", "Top-level widget"));
    return app.exec();
}
4

4 回答 4

2

要解决您的任务,您应该了解以下主题:
1. 哪些小部件可用于从用户那里获取输入(其中大部分已经说明);
2. Qt 中的信号和插槽(使用它们您可以将提供给小部件的值分配给您的变量)。

由于这些主题相当多,我建议您参考Qt 文档(例如使用Qt 助手),因为现在您知道要搜索哪些信息,您将更容易解决您的任务

于 2012-11-23T10:12:08.307 回答
1

I guess QLineEdit is what you are looking for. However, you will need to type check yourself, if that is what you are after.

And indeed, it is not easy to find. My last Qt implementation was a while ago, and it took me a little time to find it in the Qt docs.

于 2012-11-23T10:07:46.053 回答
1

Qt 中有几个输入小部件。例如 QLineEdit、QSpinBox 和 QDoubleSpinBox。您可以创建其中的几个并将它们放在网格布局中。

于 2012-11-23T10:00:14.053 回答
0

好奇你找不到...试试QInputDialog看看它是否适合你,否则子类QDialog并根据你的喜好创建对话框。

于 2012-11-23T09:54:23.283 回答