0

这个网站真是太棒了,我要感谢任何回答我帖子的人。您可能会发现我的帖子是转发的转发,因为它再次与从 QLineEdit 检索数据有关。整整 2 天我一直在尝试解决这个问题,查看 hudge stackoverflow 数据库,但我找不到答案。

基本上我想从 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 中的代码 thisaddress 是在标头和 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

过度我得到了这个错误:

  Unsupported socket Operation

所以各位有什么答案可以帮助我吗?我已经尝试遵循该堆栈中给出的解决方案,在单击 QPushButton 时将 QLineEdit 的数据存储到 QString 中, 但它仍然无法正常工作,如果我找到让你知道的东西,我仍在努力!感谢您的回复!

4

2 回答 2

1

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

当您将事件连接到多个插槽时,我认为您不会期望特定的调用顺序。您正在按钮上连接clicked()事件_launch以填充您的 IP 地址和端口号,并调用launchserver(). 但是您需要先调用其他两个插槽。

如果您可以将其连接到单个插槽会更好,这将更新适当的成员值然后进行连接。

于 2013-03-25T02:28:35.033 回答
0

It looks like I'm either missing something, or there is confusion about what exactly is happening in the connect calls. The connect function only tells Qt what to do when a signal is emitted (usually when the user does something). It does not wait for that event to happen.

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()));
}

The following is a more straightforward implementation:

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());    
}
于 2013-03-26T19:51:04.357 回答