Dragging content of one textedit to another...??



  • hello ,
    im dragging text of one textedit say "textedit" to another textedit "textedit_2" for this im not using any drag and drop functions .this im doing in a normal way only, as and when the te2 gets text i want to send that text through tcp communication to receiver.
    here is my code...

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    sendsocket = new QTcpSocket(this);
    QString hostaddress="127.0.0.1";
    sendsocket->connectToHost(hostaddress,1234);
    QString str=ui->textEdit_2->toPlainText();------------->//this gives nothing in receiver side//
    sendsocket->write(qPrintable(str));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }



    1. You need to maintain your tcp connection correctly.
      Where the guarantee that socket is in ConnectedState when you trying to write bytes to it? You need to create slot smthg like processOnConnected() and connect "signal connected() of QTcpSocket":http://qt-project.org/doc/qt-5/qabstractsocket.html#connected to this slot. And you need slot for process "error(...) signal of QTcpSocket":http://qt-project.org/doc/qt-5/qabstractsocket.html#error-2. You can check simply can you send something though your connection "by check state":http://qt-project.org/doc/qt-5/qabstractsocket.html#state must return QAbstractSocket::ConnectedState.
    2. You always must check what returns method QTcpSocket::write - "see documentation":http://qt-project.org/doc/qt-5/qiodevice.html#write, it returns number of bytes written. Additionally "you can use":http://qt-project.org/doc/qt-5/qiodevice.html#write method instead because it'll ignore zero-terminated strings.
    3. Bad plan to maintain network logic in widgets constructor - but if it is only for practice - ok.
      Sorry for my bad english, hope this helps.

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.