简体   繁体   English

从C ++的gui内部创建的对象修改GUI

[英]Modify GUI from object created inside the gui in c++

I would like to ask about how can I add some text on the screen. 我想问一下如何在屏幕上添加一些文本。 I have button, when I click on that, I create new object. 我有一个按钮,当我单击该按钮时,将创建一个新对象。 It has a function which provide some data for me. 它具有为我提供一些数据的功能。 How can I acces that data from the gui? 如何从gui访问该数据? I cannot have a getter because it gives me data after some time(after connection to the server). 我无法使用吸气剂,因为它会在一段时间后(连接到服务器后)为我提供数据。 Is there possibilit to put text to textEdit within the object created inside of gui class? 是否可以在gui类内部创建的对象内将文本放入textEdit?

thanks 谢谢

I adding the code, explaining a little more, sry for confusing, thx for trying to help: 我添加了代码,进一步解释了一些,表示困惑,感谢提供帮助:

I have EchoClient object created inside of gui class: 我在gui类内部创建了EchoClient对象:

void Comunication::startListening(){

    if (this->client == NULL)
       {
           this->client = new EchoClient(QUrl(QStringLiteral("ws://localhost:1234")), 0);
           QObject::connect(client, &EchoClient::closed, this,  &QApplication::quit);

    }
       else
           qWarning() << "Carefull, the client is already running";
   }

then in my EchoClient i have function 然后在我的EchoClient中我有功能

  void EchoClient::onTextMessageReceived(QString message)
{
    if (!m_debug)
        qDebug() << "Message received:" << message;

HERE I would like to change the ui stuff. 在这里,我想更改ui的东西。

}

Yes, It's possible. 是的,有可能。 But your question at the moment seems too broad or unclear [at least] to me. 但是目前您的问题对我来说似乎太广泛或不清楚。 I assume that you're using Qt Creator with its designer. 我假设您正在使用Qt Creator及其设计器。 So you should be able to access a ui object (in your MainWindow class). 因此,您应该能够访问ui对象(在MainWindow类中)。 Having this object in hand you can change UI at anytime and at anywhere. 拥有此对象后,您可以随时随地更改UI。

So, first assign that text box (ie QLineEdit ) an id (for example myLineEdit ). 因此,首先为该文本框(即QLineEdit )分配一个ID(例如myLineEdit )。 Now, ui->myLineEdit gives you a QLineEdit * which is actually a reference to that text box. 现在, ui->myLineEdit给您一个QLineEdit * ,它实际上是对该文本框的引用。 So wherever you'd like to update UI, you should have that reference. 因此,无论您要更新UI的何处,都应具有该参考。 For example if you're using TCP socket programming for contacting remote services, in onReadyRead signal of a QTcpSocket , you can update the text box with data you just received: 例如,如果您使用TCP套接字编程来联系远程服务,则可以在QTcpSocket onReadyRead信号中,使用刚刚接收到的数据更新文本框:

QLineEdit *textbox = ui->myLineEdit;
textbox->setText("updated data");

I solved this problem with a getter and setter and another button 我用一个吸气剂和设置器和另一个按钮解决了这个问题

First button: start listening...creates EchoClient object, this opens the websocket, connects to websocket server. 第一个按钮:开始监听...创建EchoClient对象,这将打开websocket,连接到websocket服务器。 In case the message has arrived from server it goes to method from EchoClient class called onTextMessageRecieved, there it sets atribute message to the value of incoming message. 如果消息已从服务器到达,它将转到EchoClient类的方法onTextMessageRecieved,该方法将分配消息设置为传入消息的值。

void EchoClient::onTextMessageReceived(QString message)
{
    if (!m_debug)
        qDebug() << "Message received:" << message;

    setData(message); //setting atribute message
}

Second button: get data... this will call get method from EchoClient class. 第二个按钮:获取数据...这将从EchoClient类调用get方法。 Comunication is my gui class. 交流是我的gui类。

void Comunication::on_getData_clicked()
{
    ui->textEdit_2->setText(this->client->getData());
}

But this solution is not good enough, could yo please advice how to make it that as soon as there is new message my textEdit will be automaticly updated? 但是此解决方案还不够好,请您提出如何使我的textEdit能够自动更新的新消息吗? Should I do it in another thread? 我应该在另一个线程中执行吗? I dont have much experiences. 我没有太多经验。

Thanks. 谢谢。

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

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