QDialog::accept quits Main Application - qt

I have a ClientSocket Class which is a TcpSocket in a certain state of conversation I need to ask the user to enter a Communication password. So I've created a Dialog DG::ChallangeDialog . in DG::ChallangeDialogs ctor I've
ui->setupUi(this);
QPushButton* okButton = ui->buttonBox->button(QDialogButtonBox::Ok);
if(okButton != 0x0){
okButton->setText("Challange");
}
QObject::connect(this, SIGNAL(accepted()), this, SLOT(acceptedSlot()));
acceptedSlot again emits a signal challanged(QString)
void ChallangeDialog::acceptedSlot(){
QString text = ui->passBox->text();
emit challanged(text);
}
in ClientSocket I do
case Hallo:{
if(m->message().startsWith("welcome")){
DG::ChallangeDialog* dlg = new DG::ChallangeDialog;
dlg->setModal(true);
connect(dlg, SIGNAL(challanged(QString)), this, SLOT(challanged(QString)));
dlg->exec();
/*
DG::MessagePacket* res = new DG::MessagePacket((int)Hallo);
res->setMessage("challange");
send(res);
state = Challange;
*/
}
}break;
In ClientSocket::challange slot I send a Message challange (text) over the socket and store the password.
and I expect the Dialog to hide there and the normal socket conversation to continue. and after the Dialog is accepted or rejected the main application quits (It quits it doesn't crash). Why ?
My Application has no Other Widgets. I just works like an QCoreApplication. But still I've used QApplication cause I've some GUI Needs.

Is this the only window that is shown at this time? If so, I would guess that your QApplication instance is set to quit when the last window is closed. It is true by default.
If this is the case, you should explicitly set this to false before showing any windows.

Related

QGuiApplication::commitDataRequest: QML window is not painted

I'm trying to add quit confirmation message at OS shutdown according to this example:
https://doc.qt.io/qt-5/qsessionmanager.html#allowsInteraction
I interact with the user via QML interface. It is asynchronous, so I use slots/signals. And I use additional QEventLoop to stay inside of QGuiApplication::commitDataRequest call while interacting with the user.
The trouble is that after I shutdown OS (Windows 10) - my app prevents OS to shutdown and it's good, but its screen is just white, or contains old state without the confirmation dialog. I need to hide/restore window or change its size, or do some mouse clicks inside of it to force it repaint. When it repaint all is OK and my confirmation dialog is there.
Is this a bug? Is there a workaround?
This is the code I use:
void AppQuitConfirmationAtOsShutdownManager::onCommitDataRequest(
QSessionManager &m)
{
if (!m_ui->isConfirmationRequired())
return;
if (!m.allowsInteraction())
return;
bool isConfirmed = true;
QEventLoop loop;
qtconnect(m_ui.data(), &AppQuitConfirmationUiManager::confirmationResult,
&loop, [&](bool confirmed)
{
isConfirmed = confirmed;
loop.quit();
},
Qt::QueuedConnection);
m_ui->requestConfirmation();
loop.exec();
m.release();
if (!isConfirmed)
m.cancel();
}

How do i connect messageBox when it is clicked, to a slot?

I want to create a messagebox asking question whether a user want to play again or not. When user clicks either a button , it performs a task. The task is defined in a slot. How can i connect the button click to that slot??
QMessageBox::StandardButton reply=QMessageBox::question(this,"GAME Over-Do you want to play again?");
connect(QMessageBox,SIGNAL(buttonClicked()),this,SLOT(box());
it shows QMessageBox is a class, and is unable to connect it to that slot. I want to connect to that slot.
There is different ways to use QMessageBox. You could use blocking static functions of QMessageBox and check response like that:
QMessageBox::StandardButton reply = QMessageBox::question(this,"Title", "GAME Over-Do you want to play again?");
if(reply == QMessageBox::Yes)
{
//call your slot
//box();
qDebug() << " Yes clicked";
}
else
{
//Game over
qDebug() << "game over";
}
but this will block execution of your code until user clicks some button in message box.
If you need your code run forward without waiting for user response you could use QMessageBox in non-blocking way:
QMessageBox * msg = new QMessageBox(QMessageBox::Question, "Title", "GAME Over-Do you want to play again?", QMessageBox::Yes| QMessageBox::No, this);
connect(msg,SIGNAL(accepted()),this,SLOT(box()));
connect(msg,SIGNAL(rejected()),this,SLOT(gameover()));
msg->show();
qDebug() << "Not blocked";

Basic Qt tcp-server application only show selected clients messages

I am developing a simple tcp server with qt. There is no problem with that. But the problem is, i have listed every connected client in a listbox and i want to see the incoming data only from the selected client from the listbox but i can only see the last connected client's messages.
here is the code,
this is the constructor part
server = new QTcpServer();
client = new QTcpSocket();
connect(server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
server->listen(QHostAddress::Any, ui->txtPort->text().toInt(bool(),10));
if(server->isListening())
{
ui->statusBar->showMessage("Server Started..");
}
else
{
ui->statusBar->showMessage("Server Not Started..");
}
connect(client,SIGNAL(disconnected()),this,SLOT(client_disconnected()));
connect(ui->listWidget,SIGNAL(clicked(QModelIndex)),this,SLOT(selected_client()));
here is acceptConnection() part
client = server->nextPendingConnection();
ui->listWidget->insertItem(client_count,client->peerAddress().toString());
client_count++;
and this is the listWidget item's selected item event
ui->txtRead->clear();
selected_client_index = ui->listWidget->currentIndex().row();
connect(client, SIGNAL(readyRead()),this, SLOT(startRead()));
and lastly the startRead() part
char buffer[1024] = {0};
client->read(buffer, client->bytesAvailable());
qDebug() << buffer;
ui->txtRead->insertPlainText(buffer);
How can i select the specific client and show its messages ?
Thanks in advance.
You should not use client in startRead, but some selectedClient, that You remembers in selected_client() SLOT.
As for now, when You use client = server->nextPendingConnection(), You are losing any previous clients. You should save them into some QList<QTcpSocket*>.

How to transfer Control back to the previous form?

I am new to Qt.
I am doing a project using Qt Creator. In my project, I have one mainWindow. From the main window I start 4 screens (one after another, showing Initialization process). A new QDialog screen is opened, if there are any errors on any screen. My error screens have two Button (Retry, Continue). If i press Retry, i have to restart the initialization process over again.
eg;
void ErrorScreen1::on_Retry_pressed()
{
Screen1 *scrn = new Screen1(this);
scrn->show();
this->close();
}
In above example, it restarts the process.
Is there any way, I can start the initialization process from the point it was left?
Thanks in advance,
In common you should somehow keep the current state of your process and then you can restore it.
Have you considered making those four dialogs into a wizard with four steps? The next button could work for the continue, and you could have a retry button on each page that it makes sense, with just that page doing the retry logic necessary.
I resolved this issue by using QMessageBox as my error window.
It allowed me to start my process from the point i left.
void Screen1::ErrorMessage()
{
timer->stop();
QMessageBox *msgbox = new QMessageBox(this);
msgbox->setWindowTitle("ERROR MESSAGE");
msgbox->setText("Initialization Failed.");
msgbox->setStandardButtons(QMessageBox::Cancel | QMessageBox::Retry);
msgbox->setDefaultButton(QMessageBox::Retry);
int ret = msgbox->exec();
switch (ret)
{
case QMessageBox::Retry: timer->start(); break;
case QMessageBox::Cancel:
timer->disconnect();
ui->progressBar->setValue(0);
break;
default: break;
}
}

Qt normal status bar not showing after temporary status

Normal status message -these are always shown by an application unless a temporary message is shown. This is what I know about normal status message. So using these code on my constructor
ui.statusbar->showMessage("Temp message", 3000); // ui is the Ui::AutoGenHeaderForForm
QLabel *label = new QLabel;
ui.statusBar->addWidget(label);
label->setText("hello world");
I get that, when I run my project I get the status Temp message for 3 sec. Then I don't get the hello world back. Should the hello world come automatically after 3 sec in the position of Temp message ?
Assuming the code you show is in the constructor of your main window, the problem might be due to events not being properly processed because the event loop is not yet started at the time of the main window creation.
Try to execute the showMessage in a "delayed initialization" slot, e.g.
QLabel *label = new QLabel;
ui.statusBar->addWidget(label);
label->setText("hello world");
QTimer::singleShot ( 0, this, SLOT(delayedInit() );
void MainWindow::delayedInit()
{
ui.statusbar->showMessage("Temp message", 3000); // ui is the Ui::AutoGenHeaderForForm
}
I think the documentation is pretty clear:
The widget is located to the far left of the first permanent widget
(see addPermanentWidget()) and may be obscured by temporary messages.

Resources