-
UID:171329
-
- 注册时间2016-04-07
- 最后登录2019-05-21
- 在线时间17小时
-
- 发帖5
- 搜Ta的帖子
- 精华0
- 金钱50
- 威望15
- 贡献值0
- 好评度5
-
访问TA的空间加好友用道具
|
- #include <QtWidgets/QMainWindow>
- #include "QGridLayout"
- #include "QVBOXlAYOUT"
- #include "QPushButton"
- #include "QLineEdit"
- #include "Qlabel"
- #include <QTcpSocket>
- #include <QHostAddress>
- #include <QMessageBox>
- #include <functional>
- #include <QTcpServer>
- #include "mythread.h"
- class MyWindow : public QWidget
- {
- Q_OBJECT
- public:
- QTcpServer* server;
- MyWindow(QWidget *parent = 0);
- ~MyWindow();
- QPushButton * btn;
- QLabel* label;
- QLineEdit* edit;
- QVBoxLayout* Vbox;
- QGridLayout* grid;
- signals:
- void mainwrite(QString);
- public slots:
- void On_TestBtn_Cliked();
- void acceptConnection();
- };
- MyWindow::MyWindow(QWidget *parent)
- : QWidget(parent)
- {
-
-
-
- label = new QLabel(QStringLiteral(""));
- btn = new QPushButton("send");
- edit = new QLineEdit();
- grid = new QGridLayout();
- grid->addWidget(edit,0,0);
- grid->addWidget(btn,0,1);
- Vbox = new QVBoxLayout();
- Vbox->addLayout(grid);
- Vbox->addWidget(label);
- setLayout(Vbox);
-
-
- fuc1 fu = std::bind(&MyWindow::On_TestBtn_Cliked, this);
- connect(btn, &QPushButton::clicked, this, fu);
- server = new QTcpServer(this);
- server->listen(QHostAddress::Any, 6665);
- connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
-
- }
- void MyWindow::acceptConnection()
- {
- qDebug() << "main id:" << QThread::currentThreadId();
- QTcpSocket* clientConnection = server->nextPendingConnection();
- qDebug() << __LINE__;
- MyWork* thread_Tmp = new MyWork(clientConnection->socketDescriptor(),NULL);
- connect(this, &MyWindow::mainwrite, thread_Tmp, std::bind(&MyWork::writeslot, thread_Tmp, std::placeholders::_1));
- qDebug() << __LINE__;
-
- all.append(thread_Tmp);
-
-
- }
- MyWindow::~MyWindow()
- {
- }
-
- #include <QEvent>
- #include <QApplication>
- void MyWindow::On_TestBtn_Cliked()
- {
- qDebug() << "write fuc";
- emit mainwrite(edit->text());
-
- }
线程中接收 数据没 问题,发送数据第二次出现问题,尝试了各种方法。。。 - class MyWork :public QObject
- {
- Q_OBJECT
- public:
- QThread * m_thread;
- QTcpSocket* socket;
- int m_socketid;
- MyWork(int socketid, QObject* parent = NULL);
- ~MyWork();
- public slots:
- void readClient();
- signals:
- void wirte(QString);
- public slots :
- void writeslot(QString);
- private:
- };
- MyWork::MyWork(int socketid, QObject* parent) :QObject(parent)
- {
- socket = NULL;
- m_socketid = socketid;
- m_thread = new QThread();
- socket = new QTcpSocket();
- socket->setSocketDescriptor(this->m_socketid);
- connect(socket, SIGNAL(readyRead()), this, SLOT(readClient()), Qt::DirectConnection);
- this->moveToThread(m_thread);
- m_thread->start();
- }
- void MyWork::readClient()
- {
- qDebug() << "thread get msg id:" << QThread::currentThreadId() << "read:" << socket->readAll();
- }
- void MyWork::writeslot(QString s)
- {
- qDebug() << "writeslot id:" << QThread::currentThreadId();
- socket->write(s.toStdString().c_str());
- [backcolor=#ffffff][color=#008ef1] socket->waitForBytesWritten(); //这行代码会导致 套接字[9904]QSocketNotifier:从另一个线程通知不能启用或禁用 但是注释会导致发送过一次数据 下次就无法发送[/color][/backcolor]
- qDebug() << "wait write end";
- }
- MyWork::~MyWork()
- {
- }
帮我看看 socket->waitForBytesWritten(); //这行代码会导致 套接字[9904]QSocketNotifier:从另一个线程通知不能启用或禁用 但是注释会导致发送过一次数据 下次就无法发送的诡异问题,谢谢各位了     通过 按钮触发socket发送数据,Qobject不能跨线程调用,因此用MovetoThread了,但是问题依旧,然后尝试了QEvent,问题依旧。Qt socket只能在线程内部写入吗,必须得用底层的API实现socket吗 接收和发送的函数打印出来都是在线程中,为啥还有这个问题呐!
|