Results 1 to 4 of 4

Thread: QT doesnot show any application output

  1. #1
    Join Date
    Mar 2011
    Location
    INDIA
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QT doesnot show any application output

    Hi,
    I just did couple of changes in Fortune client examplelike
    1. changed name enableGetfortune to enablebtnConnect.
    2. Added a timer so as have socket connection automatically
    the header is
    Qt Code:
    1. #ifndef CLIENT_H
    2. #define CLIENT_H
    3.  
    4. #include <QDialog>
    5. #include <QTcpSocket>
    6. #include<Qtimer>
    7.  
    8. QT_BEGIN_NAMESPACE
    9. class QLabel;
    10. class QLineEdit;
    11. class QTcpSocket;
    12. class QNetworkSession;
    13. QT_END_NAMESPACE
    14.  
    15. //! [0]
    16. class client : public QDialog
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. client(QWidget *parent = 0);
    22. QTimer *timer;
    23.  
    24. private:
    25.  
    26. void requestNewFortune();
    27. void readFortune();
    28. void newconnect();
    29. void displayError(QAbstractSocket::SocketError socketError);
    30. void enablebtnConnect();
    31. void sessionOpened();
    32. QLabel *hostLabel;
    33. QLabel *portLabel;
    34. QLineEdit *hostLineEdit;
    35. QLineEdit *portLineEdit;
    36. QLabel *statusLabel;
    37. QPushButton *btnConnect;
    38. QPushButton *quitButton;
    39. QDialogButtonBox *buttonBox;
    40.  
    41. QTcpSocket *tcpSocket;
    42. QString joyvalue;
    43. quint16 blockSize;
    44.  
    45. QNetworkSession *networkSession;
    46. };
    47. //! [0]
    48.  
    49. #endif
    To copy to clipboard, switch view to plain text mode 


    and code is

    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3.  
    4. #include "client.h"
    5.  
    6. //! [0]
    7. client::client(QWidget *parent):
    8. QDialog(parent), networkSession(0)
    9. {
    10. hostLabel = new QLabel(tr("&Server name:"));
    11. portLabel = new QLabel(tr("S&erver port:"));
    12.  
    13. // find out which IP to connect to
    14. QString ipAddress;
    15. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    16. // use the first non-localhost IPv4 address
    17. for (int i = 0; i < ipAddressesList.size(); ++i) {
    18. if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
    19. ipAddressesList.at(i).toIPv4Address()) {
    20. ipAddress = ipAddressesList.at(i).toString();
    21. break;
    22. }
    23. }
    24. // if we did not find one, use IPv4 localhost
    25. if (ipAddress.isEmpty())
    26. ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    27.  
    28. hostLineEdit = new QLineEdit(ipAddress);
    29. portLineEdit = new QLineEdit;
    30. portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    31.  
    32. hostLabel->setBuddy(hostLineEdit);
    33. portLabel->setBuddy(portLineEdit);
    34.  
    35. statusLabel = new QLabel(tr("Run Server as well"));
    36.  
    37. btnConnect = new QPushButton(tr("Connect"));
    38. btnConnect->setDefault(true);
    39. btnConnect->setEnabled(false);
    40.  
    41. quitButton = new QPushButton(tr("Quit"));
    42.  
    43.  
    44. tcpSocket = new QTcpSocket(this);
    45. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    46.  
    47. connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
    48. connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
    49. connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
    50. connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
    51. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    52. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    53.  
    54. QGridLayout *mainLayout = new QGridLayout;
    55. mainLayout->addWidget(hostLabel, 0, 0);
    56. mainLayout->addWidget(hostLineEdit, 0, 1);
    57. mainLayout->addWidget(portLabel, 1, 0);
    58. mainLayout->addWidget(portLineEdit, 1, 1);
    59. mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
    60. mainLayout->addWidget(btnConnect, 3, 0);
    61. mainLayout->addWidget(quitButton, 3, 1);
    62. setLayout(mainLayout);
    63.  
    64. setWindowTitle(tr("Fortune client"));
    65. portLineEdit->setFocus();
    66.  
    67. QNetworkConfigurationManager manager;
    68. if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
    69. // Get saved network configuration
    70. QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
    71. settings.beginGroup(QLatin1String("QtNetwork"));
    72. const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
    73. settings.endGroup();
    74.  
    75. // If the saved network configuration is not currently discovered use the system default
    76. QNetworkConfiguration config = manager.configurationFromIdentifier(id);
    77. if ((config.state() & QNetworkConfiguration::Discovered) !=
    78. QNetworkConfiguration::Discovered) {
    79. config = manager.defaultConfiguration();
    80. }
    81.  
    82. networkSession = new QNetworkSession(config, this);
    83. connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
    84.  
    85. btnConnect->setEnabled(false);
    86. statusLabel->setText(tr("Opening network session."));
    87. networkSession->open();
    88. }
    89. }
    90.  
    91.  
    92. void client::newconnect()
    93. {
    94. timer->start();
    95.  
    96. }
    97.  
    98. void client::requestNewFortune()
    99. {
    100. btnConnect->setEnabled(false);
    101. blockSize = 0;
    102. tcpSocket->abort();
    103. tcpSocket->connectToHost(hostLineEdit->text(),
    104. portLineEdit->text().toInt());
    105. }
    106. void client::readFortune()
    107. {
    108. //! [9]
    109. QDataStream in(tcpSocket);
    110. in.setVersion(QDataStream::Qt_4_0);
    111.  
    112. if (blockSize == 0) {
    113. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    114. return;
    115. //! [8]
    116.  
    117. //! [10]
    118. in >> blockSize;
    119. }
    120.  
    121. if (tcpSocket->bytesAvailable() < blockSize)
    122. return;
    123. //! [10] //! [11]
    124.  
    125. QString nextFortune;
    126. in >> nextFortune;
    127.  
    128. if (nextFortune == joyvalue) {
    129. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    130. return;
    131. }
    132. //! [11]
    133.  
    134. //! [12]
    135. joyvalue = nextFortune;
    136. //! [9]
    137. statusLabel->setText(joyvalue);
    138. btnConnect->setEnabled(true);
    139. }
    140. void client::displayError(QAbstractSocket::SocketError socketError)
    141. {
    142. switch (socketError) {
    143. case QAbstractSocket::RemoteHostClosedError:
    144. break;
    145. case QAbstractSocket::HostNotFoundError:
    146. QMessageBox::information(this, tr("Fortune client"),
    147. tr("The host was not found. Please check the "
    148. "host name and port settings."));
    149. break;
    150. case QAbstractSocket::ConnectionRefusedError:
    151. QMessageBox::information(this, tr("Fortune client"),
    152. tr("The connection was refused by the peer. "
    153. "Make sure the fortune server is running, "
    154. "and check that the host name and port "
    155. "settings are correct."));
    156. break;
    157. default:
    158. QMessageBox::information(this, tr("Fortune client"),
    159. tr("The following error occurred: %1.")
    160. .arg(tcpSocket->errorString()));
    161. }
    162.  
    163. btnConnect->setEnabled(true);
    164. }
    165.  
    166. void client::enablebtnConnect()
    167. {
    168. btnConnect->setEnabled((!networkSession || networkSession->isOpen()) &&
    169. !hostLineEdit->text().isEmpty() &&
    170. !portLineEdit->text().isEmpty());
    171.  
    172. btnConnect->setText("Connected");
    173.  
    174.  
    175. }
    176.  
    177. void client::sessionOpened()
    178. {
    179. // Save the used configuration
    180. QNetworkConfiguration config = networkSession->configuration();
    181. QString id;
    182. if (config.type() == QNetworkConfiguration::UserChoice)
    183. id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
    184. else
    185. id = config.identifier();
    186.  
    187. QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
    188. settings.beginGroup(QLatin1String("QtNetwork"));
    189. settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
    190. settings.endGroup();
    191.  
    192. statusLabel->setText(tr("Run Server as well"));
    193.  
    194. enablebtnConnect();
    195. }
    To copy to clipboard, switch view to plain text mode 
    but is not showing me any output window nor any build issues.
    Pls check it out.

    Thanks
    Arpit

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT doesnot show any application output

    Hm,i don't actually see anything that might bring any kind of "output window" in this application...There is a label (statusLabel) that you use for displaying short status messages.
    In case is if it doesn't display anything but the intial string,use qDebug() to track your application's execution process more precisely.
    (Don't forget to include "config += console " in a .pro file )

  3. #3
    Join Date
    Mar 2011
    Location
    INDIA
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT doesnot show any application output

    Thanks for replying.

    By output window i meant that QT isn't producing my application layout. but when i remove the newconnect function in the code it produces the layout.
    The code for that is:::
    Qt Code:
    1. client::client(QWidget *parent):
    2. QDialog(parent), networkSession(0)
    3. {
    4. hostLabel = new QLabel(tr("&Server name:"));
    5. portLabel = new QLabel(tr("S&erver port:"));
    6.  
    7. // find out which IP to connect to
    8. QString ipAddress;
    9. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    10. // use the first non-localhost IPv4 address
    11. for (int i = 0; i < ipAddressesList.size(); ++i) {
    12. if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
    13. ipAddressesList.at(i).toIPv4Address()) {
    14. ipAddress = ipAddressesList.at(i).toString();
    15. break;
    16. }
    17. }
    18. // if we did not find one, use IPv4 localhost
    19. if (ipAddress.isEmpty())
    20. ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    21.  
    22. hostLineEdit = new QLineEdit(ipAddress);
    23. portLineEdit = new QLineEdit;
    24. portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    25.  
    26. hostLabel->setBuddy(hostLineEdit);
    27. portLabel->setBuddy(portLineEdit);
    28.  
    29. statusLabel = new QLabel(tr("Run Server as well"));
    30.  
    31. btnConnect = new QPushButton(tr("Connect"));
    32. btnConnect->setDefault(true);
    33. btnConnect->setEnabled(false);
    34.  
    35. quitButton = new QPushButton(tr("Quit"));
    36.  
    37. buttonBox = new QDialogButtonBox;
    38. buttonBox->addButton(btnConnect, QDialogButtonBox::ActionRole);
    39. buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
    40.  
    41.  
    42. tcpSocket = new QTcpSocket(this);
    43. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    44.  
    45. connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
    46. connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
    47. \\ connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
    48. connect(btnConnect, SIGNAL(clicked()), this, SLOT(requestNewFortune()));
    49. \\connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
    50. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    51. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    52.  
    53. QGridLayout *mainLayout = new QGridLayout;
    54. mainLayout->addWidget(hostLabel, 0, 0);
    55. mainLayout->addWidget(hostLineEdit, 0, 1);
    56. mainLayout->addWidget(portLabel, 1, 0);
    57. mainLayout->addWidget(portLineEdit, 1, 1);
    58. mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
    59. mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
    60. setLayout(mainLayout);
    61.  
    62. setWindowTitle(tr("Fortune client"));
    63. portLineEdit->setFocus();
    64.  
    65. QNetworkConfigurationManager manager;
    66. if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
    67. // Get saved network configuration
    68. QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
    69. settings.beginGroup(QLatin1String("QtNetwork"));
    70. const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
    71. settings.endGroup();
    72.  
    73. // If the saved network configuration is not currently discovered use the system default
    74. QNetworkConfiguration config = manager.configurationFromIdentifier(id);
    75. if ((config.state() & QNetworkConfiguration::Discovered) !=
    76. QNetworkConfiguration::Discovered) {
    77. config = manager.defaultConfiguration();
    78. }
    79.  
    80. networkSession = new QNetworkSession(config, this);
    81. connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
    82.  
    83. btnConnect->setEnabled(false);
    84. statusLabel->setText(tr("Opening network session."));
    85. networkSession->open();
    86. }
    87. }
    88.  
    89.  
    90. \\void client::newconnect()
    91. \\{
    92. \\timer->start(500);
    93.  
    94. \\}
    To copy to clipboard, switch view to plain text mode 

    i also did debugging & it gave error as ::

    the inferior stopped because it received a signal from operating system
    Signal :SIGSEGV
    Signal meaning: segmentation fault

  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT doesnot show any application output

    Hm,do you recieve the error signal all the time ? Are there any debug messages when you run your application with all the slots connected ?
    I mean,it seems impossible that timer activation can somehow affect widget's layout,there's got to be smth. else,i guess.

    P.S:by the way,i don't see the line where you initialize the timer object in your class constructor...

  5. The following user says thank you to AlexSudnik for this useful post:

    Arpitgarg (27th March 2011)

Similar Threads

  1. Replies: 3
    Last Post: 20th October 2010, 22:36
  2. Core Application - Input/Output
    By Peppy in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2010, 11:47
  3. Automatic clearing of Application Output Pane
    By piotr.dobrogost in forum Qt Tools
    Replies: 0
    Last Post: 4th August 2009, 20:56
  4. font size in Application Output Pane
    By piotr.dobrogost in forum Qt Tools
    Replies: 0
    Last Post: 4th August 2009, 20:55
  5. Replies: 1
    Last Post: 24th November 2008, 09:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.