单击QPushButton时,将数据从QLineEdit转换为QString。

时间:2013-03-25 01:22:30

标签: c++ qt4

这个网站非常棒,我想感谢任何回答我帖子的人。您可能会发现我的帖子重新发布了一个转贴,因为它再次涉及来自QLineEdit的转发数据。我一直试图通过hudge stackoverflow数据库解决问题2天,但我找不到答案。

基本上我想从QLineEdit中检索一个ip地址,并从QLineEdit中检索一个端口号,这样做:

myclass::myclass(QWidget *parent = 0)
{
     _mainuilayout = new QGridLayout();
     ipAddress = new QLineEdit();
     portnumber = new QLineEdit();
     QFormLayout *connect2adress = new QFormLayout();
     connect2adress->addRow("Ip Adress : ", ipAddress);
     connect2adress->addRow("Port number : ", portnumber);
     _launch = new QPushButton("Launch server");
     _mainuilayout->addWidget(_launch);
     _mainuilayout->addLayout(connect2adress);

     QObject::connect(_launch, SIGNAL(clicked()), this, SLOT(setipAddress()));
     QObject::connect(_launch, SIGNAL(clicked()), this, SLOT(setportnumber()));

     server->connectTo(thisaddress,thisport);

     QObject::connect(_launch, SIGNAL(clicked()), server, SLOT(launchserver()));
}

这里有代码将IP地址存储在QString中这个地址是在标题和thisport中定义的QString

void myclass::setipAddress()
{
     thisaddress = ipAddress->text();
}

void myclass::setportnumber()
{
    thisport = portnumber->text().toShort();
}

我想要的是当我点击启动按钮时,它将来自QLineEdit的数据存储在此地址和此地址中,以便我可以启动服务器,这里是connectTo的代码

void server::connectTo(QString ipAdress,quint16 port)
{
    if(!ipAdress.isEmpty() && port != 0 )
    {
         ipAddress = ipAdress;
         portnumber = port;
    }
}

当我将QLineEdit转换为QString时问题真的来了,因为当我直接分配thisAddress和thisportnumber时它正在工作

  thisAddress = "127.0.0.1"
  thisportnumber = 5855

Overwise我收到了这个错误:

  Unsupported socket Operation

那么伙计们你有什么答案可以帮助我吗? 我试图遵循该帖子Store QLineEdit's data into a QString upon a QPushButton click中给出的解决方案 但它仍然无法正常工作如果我找到了一些让你知道的东西我还在努力! 谢谢你的回复!

2 个答案:

答案 0 :(得分:1)

自从我使用Qt以来已经很久了,但我看到没有人回答你的问题所以我会加上我的两分钱。

当您将事件连接到多个广告位时,我认为您不能指望特定的通话订单。您正在clicked()按钮上连接_launch事件,以填充您的IP地址和端口号,并呼叫launchserver()。但是你需要先调用另外两个插槽。

如果你只需将它连接到一个插槽就会更好,这将更新相应的成员值,然后进行连接。

答案 1 :(得分:0)

看起来我要么缺少某些东西,要么对连接调用中究竟发生了什么感到困惑。 connect函数只告诉Qt发出信号时要做什么(通常在用户做某事时)。它不会等待该事件发生。

myclass::myclass(QWidget *parent = 0)
{
     // <snipped construction and layout>

     // Tell Qt what to do when the clicked signal happens:
     QObject::connect(_launch, SIGNAL(clicked()), this, SLOT(setipAddress()));
     QObject::connect(_launch, SIGNAL(clicked()), this, SLOT(setportnumber()));

     // At this point, nothing has been set, because the user hasn't had time to
     // fill in anything -- you've only told the application what to do when they
     // do so.
     server->connectTo(thisaddress,thisport);

     // As pointed out previously, even though you're telling the application what
     // to do, the application says nothing about the order, so you don't know if
     // this will be run before or after the slots to set the ip address and port
     // number.
     // What you do know is that it won't be run before the previous line of code,
     // which does the first connection.
     QObject::connect(_launch, SIGNAL(clicked()), server, SLOT(launchserver()));
}

以下是一个更直接的实现:

myclass::myclass(QWidget *parent = 0)
{
     _mainuilayout = new QGridLayout();
     _ipAddress = new QLineEdit();
     _portnumber = new QLineEdit();
     QFormLayout *connect2adress = new QFormLayout();
     connect2adress->addRow("Ip Adress : ", ipAddress);
     connect2adress->addRow("Port number : ", portnumber);
     _launch = new QPushButton("Launch server");
     _mainuilayout->addWidget(_launch);
     _mainuilayout->addLayout(connect2adress);

     QObject::connect(_launch, SIGNAL(clicked()), server, SLOT(launchserver()));
}

myclass::launchserver()
{
     server->connectTo(_ipAddress->text(),_portnumber->text().toShort());    
}