How to extend QClipboard? - c++

How can I extend QClipboard to allow selection of all text in all open windows.
Please provide code.
Thanks

See QClipboard it's very simple for just text
QClipboard *clipboard = QApplication::clipboard();
QString getCLipboardText = clipboard->text();
QString newClipboardText("blah blah");
clipboard->setText(newClipboardText);

Related

QFileDialog combine MIME type filters to "All formats"

I am using Qt 5.9 to open a file dialog asking the user to select an image file:
QStringList mimeTypeFilters;
const QByteArrayList supportedMimeTypes = QImageReader::supportedMimeTypes();
foreach(const QByteArray& mimeTypeName, supportedMimeTypes) {
mimeTypeFilters.append(mimeTypeName);
}
mimeTypeFilters.sort();
QFileDialog* fileDialog = new QFileDialog(this, "Select image");
fileDialog->setMimeTypeFilters(mimeTypeFilters);
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->exec();
All supported image formats are added as MIME type filters to the dialog, which is working quite well. However, I want to add an additional filter (such as "All formats" or "All supported") that allows the user to select an image of ANY of the supported formats, as selecting the correct format prior to selecting the image is quite tedious. What is the most elegant solution to achieve this, without subclassing any of the involved Qt classes?
Thanks to SteakOverflow's comment, here is the solution I came up with:
// get supported image file types
QStringList mimeTypeFilters;
const QByteArrayList supportedMimeTypes = QImageReader::supportedMimeTypes();
foreach(const QByteArray& mimeTypeName, supportedMimeTypes) {
mimeTypeFilters.append(mimeTypeName);
}
mimeTypeFilters.sort(Qt::CaseInsensitive);
// compose filter for all supported types
QMimeDatabase mimeDB;
QStringList allSupportedFormats;
for(const QString& mimeTypeFilter: mimeTypeFilters) {
QMimeType mimeType = mimeDB.mimeTypeForName(mimeTypeFilter);
if(mimeType.isValid()) {
allSupportedFormats.append(mimeType.globPatterns());
}
}
QString allSupportedFormatsFilter = QString("All supported formats (%1)").arg(allSupportedFormats.join(' '));
QFileDialog* fileDialog = new QFileDialog(this, "Select image");
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->setMimeTypeFilters(mimeTypeFilters);
QStringList nameFilters = fileDialog->nameFilters();
nameFilters.append(allSupportedFormatsFilter);
fileDialog->setNameFilters(nameFilters);
fileDialog->selectNameFilter(allSupportedFormatsFilter);
It is basically the same implementation QFileDialog uses internally to convert mime type filters to name filters. The new name filter for all supported formats will be added at the bottom of the filter list and pre-selected. The filter string is quite long and not fully visible in the dialog at once, but will become fully visible once the user opens the drop-down menu.

Qt: add a file selection field on the form (QLineEdit and "browse" button)

I need to display QLineEdit with "Browse" button at my form. When user clicks button, QFileDialog should be opened, and so on.
This is pretty common thing, but I can't find ready-made solution for that. I expected in Qt Designer some widget like QFileSelect, or something like that, but found nothing similar.
Should I implement it by hand? Or, what is the correct way to do this?
Should I implement it by hand? Or, what is the correct way to do this?
Yes, I agree with you that it is a common thing, but unfortunately you will need to implement this yourself. The good news is that you can do this easily by something like this:
MyMainWindow::createUI()
{
label = new QLabel("foo");
button = new QPushButton("Browse");
connect(button, SIGNAL(clicked()), SLOT(browse()));
layout = new QHorizontalLayout();
layout->addWidget(label);
layout->addWidget(button);
setLayout(layout);
}
void MyMainWindow::browse()
{
QString directory = QFileDialog::getExistingDirectory(this,
tr("Find Files"), QDir::currentPath());
if (!directory.isEmpty()) {
if (directoryComboBox->findText(directory) == -1)
directoryComboBox->addItem(directory);
directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
}
}

QPrinter's PDF data, how to grab it?

As the title says, how do i grab the internal PDF data from QPrinter without going the extra mile of outputting to a temporary file and reading that in for further processing?
I have checked the documentation and found nothing what would let me do something like
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = printer.data();
Any ideas? :)
The only way I know would be by using QTemporaryFile
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTemporaryFile pdf;
pdf.open();
printer.setOutputFileName(pdf.fileName());
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = pdf.readAll();

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() ) );

How do I print a variable value in QMessageBox?

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);