How do I print a variable value in QMessageBox? - c++

Do I need to use a QString first then put it in the msgbox? Are there any examples?

The QMessageBox documentation has examples in it:
QMessageBox msgBox;
msgBox.setText("Put your text here");
msgBox.exec();
There are a few others in there. Please read the docs.

you can use the folowing example and you can add as many as arguments you want.
int device_count=0;
QString status = QString("Found %1 device(s):").arg(device_count);
QMessageBox::information(this, tr("Info"), status);

Related

How to I make QMessageBox delete the content of a vector

I'm very new to code so I apologise if this is a simple question but I am struggling.
I have a GUI program that saves user input into a vector, displays it then can be saved as a txt file. Once it has been saved, I want a QMessageBox to appear asking if the user wishes to delete the now saved vector data. The vector is named v_History.
QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
msgBox.exec();
Any help or advice is greatly appreciated.
Thank you
When using setStandardButtons(), exec() returns a value indicating which standard button was clicked, eg:
QMessageBox msgBox;
msgBox.setText("History saved to file.");
msgBox.setInformativeText("Would you like to delete the current history?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard);
msgBox.setDefaultButton(QMessageBox::Discard);
int ret = msgBox.exec();
if (ret == QMessageBox::Discard)
{
// delete the history as needed ...
}

How to use a custom validation function for a QCompleter in a QComboBox

I have a string matching function to be used for searching for names that is more advanced than QString::contains() (e. g. when you search for "mueller", it will match "Müller").
I'd like to use this function to search inside a QComboBox. The default completion almost does what I need: If I do
combobox->setEditable(true);
combobox->setInsertPolicy(QComboBox::NoInsert);
combobox->completer()->setCompletionMode(QCompleter::PopupCompletion);
and type some text in the QComboBox's lineedit, the popup pops up, only showing entries starting what has been typed.
This is what I want, but I would like the QCompleter to evaluate matches using my search function rather than the QString::startsWith() that is apparently used here (and setting the mode to Qt::MatchContains is better but still not sufficient).
Is there any way to customize the completer's search function?
Thanks for all help!
I ended up using an own QCompleter and set it for the QComboBox's QLineEdit. The completer does not use the combobox's model, but an own one, which is filled with data everytime the entered text changes.
Can be done as follows:
m_matchingNames = new QStringListModel(this);
m_nameCompleter = new QCompleter(m_matchingNames, this);
m_nameCompleter->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
m_playersSelect->setEditable(true);
m_playersSelect->setInsertPolicy(QComboBox::NoInsert);
m_playersSelect->setCompleter(0);
m_playersSelect->lineEdit()->setCompleter(m_nameCompleter);
connect(m_playersSelect->lineEdit(), &QLineEdit::textEdited, this, &ScorePage::nameSearchChanged);
and
void ScorePage::nameSearchChanged(const QString &text)
{
QStringList possibleNames;
for (const QString &name : m_availableNames) {
if (checkMatch(name, text)) {
possibleNames << name;
}
}
m_matchingNames->setStringList(possibleNames);
}
Most probably not the most prerformant solution, but it works :-)
One then can also connect to QCompleter::activated() to process what has been chosen from the list and e. g. do a QComboBox::setCurrentIndex() or such.

How can I verify if the checkbox is checked or not in Qt?

The following code is supposed to set the text of nameLine form to this box is unchecked when the QCheckBox instance checkbox has state Unchecked.
Here is the my checkbox instance declaration:
QCheckBox *checkbox = new QCheckBox("paid with cash!", this);
checkbox->setCheckState(Qt::Unchecked);
and here is the logic so far:
if(checkbox->checkState(Qt::Unchecked))
{
nameLine->setText("the box is unchecked");
}
This code does not compile. The resulting error is the following:
C:\Qt\5.1.1\mingw48_32\examples\widgets\tutorials\addressbook\part1\voruskra.cpp:144: error: no matching function for call to 'QCheckBox::checkState(Qt::CheckState)'
if(checkbox->checkState(Qt::Unchecked))
^
Can you tell me what I am doing wrong?
Unless you are using a tristate checkbox, you can simply if (checkbox->isChecked())
This property is inherited way back in QAbstractButton. If it is a tristate checkbox, you will have to use checkState() as suggested in the other answer.
I think checkState doesn't take any argument. Try if(checkbox->checkState() == Qt::Unchecked)
maybe you can try like this?
QCheckBox *checkbox = new QCheckBox("paid with cash!", this);
checkbox->setChecked(false);
then for if command..
if(!checkbox->isChecked)
{
nameLine->setText("the box is unchecked");
}

live changing as typing in QLineEdit

I have a QlineEdit and a QTableView in a simple program.
I load a table (for example person) from SQLite to tableView.
I want an event or anything else that as I type in lineEdit the tableView change based on it.
For example if the the table person have a field name that filled by:
mehran
mehsa
mahid
naser
omid
I want when I press "m" all the name that started with "m", like mehran, mehsa, mahid show on the tableView. And when I press next key for example "e", just mehran and mehsa show on tableView, and so on.
You would need to do a connection like this based on this signal:
connect(lineEdit, &QLineEdit::textChanged, [=](const QString &string) {
QSqlQuery query(QString("SELECT %1 FROM ..").arg(string));
while (query.next()) {
QStringList stringList = query.value(0).toStringList();
updateTableView(stringList);
}
});
At this point in time, you will also need to add the following line in your qmake project file to get the new signal-slot syntax:
CONFIG += c++11

Howto open html page in default browser, when html code is stored in QString

My Qt application should open a html page (with the default browser eg. IE). This html code is stored in a QString.
What would be the best way to open this "file", of which I only have the content?
Is QTemporaryFile the answer to this? Or could this be done easier?
QString content = "<html>...</html>";
?
QDesktopServices::openUrl(QUrl("..."));
The QTemporaryFile approach is by far the easiest to accomplish your task.
I don't see any other way other then doing some "vodoo" with ActiveQt, if that works at all.
Best regards.
EDIT: An example
QString htmlData; // your HTML data here
// The six Xs are actually required.
QTemporaryFile tmpFile( QLatin1String( "thefileXXXXXX.html" ) );
tmpFile.open();
QTextStream out( &tmpFile )
out << htmlData;
tmpFile.close();
QDesktopServives::openUrl( QUrl::fromLocalFile( tmpFile.fileName() ) );