PDA

View Full Version : QT doesnot show any application output



Arpitgarg
26th March 2011, 17:35
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


#ifndef CLIENT_H
#define CLIENT_H

#include <QDialog>
#include <QTcpSocket>
#include<Qtimer>

QT_BEGIN_NAMESPACE
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QNetworkSession;
QT_END_NAMESPACE

//! [0]
class client : public QDialog
{
Q_OBJECT

public:
client(QWidget *parent = 0);
QTimer *timer;

private:

void requestNewFortune();
void readFortune();
void newconnect();
void displayError(QAbstractSocket::SocketError socketError);
void enablebtnConnect();
void sessionOpened();
QLabel *hostLabel;
QLabel *portLabel;
QLineEdit *hostLineEdit;
QLineEdit *portLineEdit;
QLabel *statusLabel;
QPushButton *btnConnect;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;

QTcpSocket *tcpSocket;
QString joyvalue;
quint16 blockSize;

QNetworkSession *networkSession;
};
//! [0]

#endif



and code is


#include <QtGui>
#include <QtNetwork>

#include "client.h"

//! [0]
client::client(QWidget *parent):
QDialog(parent), networkSession(0)
{
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port:"));

// find out which IP to connect to
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();

hostLineEdit = new QLineEdit(ipAddress);
portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535, this));

hostLabel->setBuddy(hostLineEdit);
portLabel->setBuddy(portLineEdit);

statusLabel = new QLabel(tr("Run Server as well"));

btnConnect = new QPushButton(tr("Connect"));
btnConnect->setDefault(true);
btnConnect->setEnabled(false);

quitButton = new QPushButton(tr("Quit"));


tcpSocket = new QTcpSocket(this);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));

QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(btnConnect, 3, 0);
mainLayout->addWidget(quitButton, 3, 1);
setLayout(mainLayout);

setWindowTitle(tr("Fortune client"));
portLineEdit->setFocus();

QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequir ed) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();

// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}

networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));

btnConnect->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
}
}


void client::newconnect()
{
timer->start();

}

void client::requestNewFortune()
{
btnConnect->setEnabled(false);
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),
portLineEdit->text().toInt());
}
void client::readFortune()
{
//! [9]
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
//! [8]

//! [10]
in >> blockSize;
}

if (tcpSocket->bytesAvailable() < blockSize)
return;
//! [10] //! [11]

QString nextFortune;
in >> nextFortune;

if (nextFortune == joyvalue) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}
//! [11]

//! [12]
joyvalue = nextFortune;
//! [9]
statusLabel->setText(joyvalue);
btnConnect->setEnabled(true);
}
void client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune client"),
tr("The following error occurred: %1.")
.arg(tcpSocket->errorString()));
}

btnConnect->setEnabled(true);
}

void client::enablebtnConnect()
{
btnConnect->setEnabled((!networkSession || networkSession->isOpen()) &&
!hostLineEdit->text().isEmpty() &&
!portLineEdit->text().isEmpty());

btnConnect->setText("Connected");


}

void client::sessionOpened()
{
// Save the used configuration
QNetworkConfiguration config = networkSession->configuration();
QString id;
if (config.type() == QNetworkConfiguration::UserChoice)
id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
else
id = config.identifier();

QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
settings.endGroup();

statusLabel->setText(tr("Run Server as well"));

enablebtnConnect();
}

but is not showing me any output window nor any build issues.
Pls check it out.

Thanks
Arpit

AlexSudnik
27th March 2011, 08:35
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 )

Arpitgarg
27th March 2011, 12:12
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:::

client::client(QWidget *parent):
QDialog(parent), networkSession(0)
{
hostLabel = new QLabel(tr("&Server name:"));
portLabel = new QLabel(tr("S&erver port:"));

// find out which IP to connect to
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 address
for (int i = 0; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhost
if (ipAddress.isEmpty())
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();

hostLineEdit = new QLineEdit(ipAddress);
portLineEdit = new QLineEdit;
portLineEdit->setValidator(new QIntValidator(1, 65535, this));

hostLabel->setBuddy(hostLineEdit);
portLabel->setBuddy(portLineEdit);

statusLabel = new QLabel(tr("Run Server as well"));

btnConnect = new QPushButton(tr("Connect"));
btnConnect->setDefault(true);
btnConnect->setEnabled(false);

quitButton = new QPushButton(tr("Quit"));

buttonBox = new QDialogButtonBox;
buttonBox->addButton(btnConnect, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);


tcpSocket = new QTcpSocket(this);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

connect(hostLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
connect(portLineEdit, SIGNAL(textChanged(QString)),this, SLOT(enablebtnConnect()));
\\ connect(btnConnect, SIGNAL(clicked()), this, SLOT(newconnect()));
connect(btnConnect, SIGNAL(clicked()), this, SLOT(requestNewFortune()));
\\connect(timer, SIGNAL(timeout()),this, SLOT(requestNewFortune()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));

QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(hostLabel, 0, 0);
mainLayout->addWidget(hostLineEdit, 0, 1);
mainLayout->addWidget(portLabel, 1, 0);
mainLayout->addWidget(portLineEdit, 1, 1);
mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
setLayout(mainLayout);

setWindowTitle(tr("Fortune client"));
portLineEdit->setFocus();

QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequir ed) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();

// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}

networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));

btnConnect->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
}
}


\\void client::newconnect()
\\{
\\timer->start(500);

\\}



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

AlexSudnik
27th March 2011, 12:45
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...