简体   繁体   English

Qt C ++; 如何解决特定的文本字段,具体取决于用户输入

[英]Qt C++; How to address a certain textfield, depending on user input

I'm having issues with formulating the question but this is what I want to do with my application. 我在提出问题时遇到问题,但这是我想对我的应用程序进行的处理。

A user can select one or multiple image-files (.ppm), and they are displayed in some sort of legend, with their filename underneath. 用户可以选择一个或多个图像文件(.ppm),它们以某种图例形式显示,其文件名位于下面。 The information of these images is stored in a structure. 这些图像的信息存储在结构中。 (This structure contains the image path, name, and other info). (此结构包含图像路径,名称和其他信息)。

Now I want to give the user the chance to change the name of the selected images, and uses this name in the rest of the application. 现在,我想让用户有机会更改所选图像的名称,并在应用程序的其余部分中使用该名称。 So I would have to change the name in the structure. 因此,我将不得不在结构中更改名称。

I could do this by adding textfields in the legend, where users can type the desired name, but how can I get the input from this textfield if I don't know which one is alterred? 我可以通过在图例中添加文本字段来实现此目的,用户可以在其中输入所需的名称,但是如果我不知道哪个字段被更改,如何从该文本字段获取输入?

If the user selects 6 images, I need 6 new textfields in the legend, but how can I address the correct one? 如果用户选择6张图像,则图例中需要6个新的文本字段,但是如何解决正确的文本字段?

 struct[2].name = input2.getText();

I also thought about doing it with some sort of wizard, with 6 pages where the names can be changed, but I don't know how I can adress the correct textfield. 我还考虑过使用某种向导进行操作,该向导有6页可以更改名称,但是我不知道如何找到正确的文本字段。

Any help would be welcome, thanks!! 任何帮助都将受到欢迎,谢谢!

If you want to allow users to rename multiple files at one time, you may want to create a wizard. 如果要允许用户一次重命名多个文件,则可能需要创建一个向导。 In the wizard you could display each picture they selected (one at a time) and allow them to rename each picture (one at a time). 在向导中,您可以显示他们选择的每张图片(一次),并允许他们重命名每张图片(一次)。 Otherwise it will be confusing to the user and harder for you to manage. 否则,它将使用户感到困惑,并且使您难以管理。

When generating the wizard, I would use the information structure to associate the picture with the textfield. 生成向导时,我将使用信息结构将图片与文本字段关联。

Qt, signals and slots are your friend here. Qt,信号和插槽是您的朋友。

When you setup the textfields for the name, assuming you use something like a QLineEdit object, connect to a relevant signal, such as editingFinished(). 在设置名称的文本字段时,假设您使用的是QLineEdit对象,请连接到相关信号,例如editFinished()。 Make the connection to the slot of the Object that stores the structure. 连接到存储结构的对象的插槽。 The receiving slot then updates the appropriate information. 然后,接收插槽将更新相应的信息。

So, assuming your struct is in an object derived from QObject, you can do something like this: - 因此,假设您的结构位于QObject派生的对象中,则可以执行以下操作:-

struct DataStruct // the struct storing the underlying data
{
    QString name;
    QLineEdit* linkedEditWidget; // widget for user to change text
};


class MainObject : public QObject
{
    Q_OBJECT // required for signals and slots

    public slots:
        void UpdateText();

    private:
     const int NUM_STRUCTS = 10; // initialisation in C++ 11
     DataStruct myStructs[NUM_STRUCTS]; // a number of structs
};

When you initialize the array of structs and the LineEdit widgets, store a pointer to the matching LineEdit widget in each myStruct and connect the widgets' editingFinished signals to the MainObject updateText() slot. 初始化结构数组和LineEdit小部件时,将指向匹配的LineEdit小部件的指针存储在每个myStruct中,并将小部件的editFinished信号连接到MainObject updateText()插槽。 It would be a good idea to use weak smart pointers here, but I'll use a standard pointer, to keep things simple. 在这里使用弱智能指针是一个好主意,但是我将使用标准指针来简化操作。

When you receive notification that the text has changed, you'll need to match up the caller with the LineEdit* in the struct. 收到文本已更改的通知时,您需要将调用方与结构中的LineEdit *相匹配。 Note that QObject::sender() will return the pointer to the object that sent the message: - 请注意,QObject :: sender()将返回指向发送消息的对象的指针:-

void MainObject::UpdateText()
{
    QObject* theSendingWidget = sender();
    for(int i=0; i<NUM_STRUCTS; ++i) // assuming NUM_STRUCTS is already defined
    {
        if(myStructs[i].linkedEditWidget == theSendingWidget)
        {
            // update the name in the data struct
            myStructs[i].name = (static_cast<QLineEdit*>(theSendingWidget))->text();
            return; // our work is done.
        }
    }
}

Finally, you'd probably make life easier for yourself by storing the data in Qt model objects, rather than using a plain struct. 最后,您可以通过将数据存储在Qt模型对象中而不是使用普通结构来使自己的生活更轻松。 I suggest reading up on Model / View programming and the MVC design pattern 我建议阅读Model / View编程MVC设计模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM