QT Breadcrumb Navigation - c++

I would like to add a breadcrumb navigation bar to a QT application. In KDE there is KUrlNavigator which is used by dolphin. Is there a native QT widget?

qxtcrumbview in libqxt.org
Not documented, you need to download the source code in order to find this one.

This may be achieved with QLabel: There may be multiple links in a single QLabel, see QLabel::openExternalLinks() and Qt::TextInteractionFlags.
Or in more detail:
auto label = new QLabel();
label->setText(
"<a href=\"https://doc.qt.io/qt-5/qlabel.html\">"
"QLabel</a>::"
"<a href=\"https://doc.qt.io/qt-5/qlabel.html#textInteractionFlags-prop\">"
"textInteractionFlags</a>");
label->setTextFormat(Qt::RichText);
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
label->setOpenExternalLinks(true);

Related

How to grey out a gtkmm menu item that cannot be clicked on

I am using the library gtkmm in C++. This is the part of the code where I define "Open":
Gtk::ImageMenuItem *menuOpen = Gtk::manage(new Gtk::ImageMenuItem(Gtk::Stock::OPEN));
menuFile->append(*menuOpen);
I want "Open" to be greyed out if I cannot click on it, but I don't know the method which allows to do that. Any suggestions?
Thank you for your help.
In gtkmm 3 both Gtk::ImageMenuItem and Gtk::Stock have been deprecated, so it is best not to use them. Just use Gtk::MenuItem only with the text set to "_Open".
All widgets in gtkmm derive from Gtk::Widget. The method to use is Gtk::Widget::set_sensitive(bool).
To grey out or make insensitive your menu, use:
menuOpen->set_sensitive(false);
To re-enable the menu item:
menuOpen->set_sensitive();
If you want to find out if it is greyed out or not, use:
bool am_i_sensitive = menuOpen->get_sensitive();

Use a system icon in the setIcon method of QWidget

I would like cross-platform a way to get a system icon, for example the folder icon in the setIcon method of QWidget. Something like this:
QWidget *myWidget = new QWidget;
myWidget->setIcon(/*something to get a system icon*/);
Is this possible? If it is, how to do it?
Use QFileIconProvider::icon(IconType) to get an icon of a particular kind, from a small selection of types.
In your particular case, you'd want icon(QFileIconProvider::Folder).
Use QFileIconProvider::icon(const QFileInfo &) to get an icon for a particular directory entry.
Use QIcon::fromTheme(const QString &, const QIcon & = QIcon()) to get a theme icon on Linux/X11.
I think you're looking for QIcon::fromTheme method.
Example:
QIcon undoicon = QIcon::fromTheme("edit-undo");
Note: By default, only X11 will support themed icons. In order to use themed icons on Mac and Windows, you will have to bundle a compliant theme in one of your themeSearchPaths() and set the appropriate themeName().
For linux/X11, maybe you are looking for QMimeDatabase::mimeTypesForFileName and QIcon::fromTheme.
Refer to https://stackoverflow.com/a/45739529/5785726 for a example

QWebEngine: print a page?

The migration from QWebKit to QWebEngine seems to be much more complicated than Qt guys claimed. With QWebKit I could print a webpage easily via
QWebView->print(&printer);
With QWebEngine class QWebEngine view does not provide a method print(). Their browser example uses a class named QWebEngineFrame which offers a method print() - but the whole QWebEngineFrame is not defined anywhere!
So my question: how do I print a page using QWebEngine?
I think the correct way to use QWebEngineView::render method because QWebEngineView is a QWidget. It accepts paint device as a first argument and you may pass QPrinter there for printing.
Update: If you can use the latest version of Qt, in Qt 5.8 a new function for printing page was added:
void QWebEnginePage::print(QPrinter *printer, FunctorOrLambda resultCallback);
Actually it first prints to temp PDF with QPrinter settings.
Here is the link to Qt docs.
You can read about this in our blog also.
I would offer following code (for a while):
QTextEdit *textEdit = new QTextEdit;
ui.myWebView->page()->toHtml([textEdit](const QString &result){ textEdit->setHtml(result); });
textEdit->print(somerinter);
textEdit->deleteLater();
Qt 5.7 includes print support in to pdf files for QWebEngine.
Use printToPdf function to export the current page in a pdf file. Example:
const QString fileName = QFileDialog::getSaveFileName(0,
tr("Save pdf"),
".",
tr("PDF Files (*.pdf)"));
if (fileName.isEmpty()) {
return;
}
ui->webView->page()->printToPdf(fileName);
QWebView->page()->print(&printer, [=](bool){});

Qt: How can I access the actual widgets on a page in WebKit?

Is there a way to access the widgets generated by INPUT and SELECT on a page in WebKit, using Qt?
On a related note, does WebKit provide these widgets, or does it delegate back to Qt to generate them?
There are no "widgets". Newer browsers render all elements themselves to allow overlays etc.
If you want to manipulate them use the DOM.
Everything inside in QWebView does not use the conventional Qt widget system. It's only HTML, rendered by WebKit. But you can access to html by using the evalJS function. Example of code:
QString Widget::evalJS(const QString &js)
{
QWebFrame *frame = ui->webView->page()->mainFrame();
return frame->evaluateJavaScript(js).toString();
}
evalJS(QString("document.forms[\"f\"].text.value = \"%1\";").arg(fromText));
evalJS(QString("document.forms[\"f\"].langSelect.value = \"%1\";").arg(langText));
evalJS(QString("translate()"));
QString from = evalJS("document.forms[\"f\"].text.value");
QString translation = evalJS("document.forms[\"f\"].translation.value");
ui->textEditTo->setText(translation);

An "About" message box for a GUI with Qt

QMessageBox::about( this, "About Application",
"<h4>Application is a one-paragraph blurb</h4>\n\n"
"Copyright 1991-2003 Such-and-such. "
"For technical support, call 1234-56789 or see\n"
"http://www.such-and-such.com" );
This code is creating the About message box which I wanted to have with two exceptions:
1) I would like to change the icon in the message box with an aaa.png file
2) And I would like to have the link clickable. It looks like hyperlink (it is blue and underlined) but mouse click does not work
Any ideas?
I think you should create a custom QWidget for your about widget. By this way, you can put on the widget all you want. By example, you can place QLabel using the openExternalLinks property for clickable link.
To display a custom image on the QWidget, this example may help.
For the icon, you need to just set the application icon. Something like this:
QApplication::setWindowIcon(QIcon(":/aaa.png")); // from a resource file
As for making the links clickable, I don't think it can be done with the QMessageBox::about API directly.
QMessageBox msgBox;
msgBox.setTextFormat(Qt::RichText); // this does the magic trick and allows you to click the link
msgBox.setText("Text<br />http://www.such-and-such.com");
msgBox.setIcon(yourIcon);
msgBox.exec();
For future reference, the docs state that the default type for textFormat is Qt::AutoText. The docs further state that Qt::AutoText is interpreted as Qt::RichText if Qt::mightBeRichText() returns true, otherwise as Qt::PlainText. Finally, mightBeRichText uses a fast and therefore simple heuristic. It mainly checks whether there is something that looks like a tag before the first line break. So, since you dont have a tag in your first line, it assumes that it is plain text. Set it to RichText explicitely with msgBox.setTextFormat(Qt::RichText); to make it act accordingly.
there's a message in the qtcenter about it:
http://www.qtcentre.org/threads/17365-Clickable-URL-in-QMessageBox
Use http://doc.qt.nokia.com/latest/qlabel.html#setOpenExternalLinks
main.cpp
QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/images/your_icon.png"));
mainwindow.cpp (into your slot if you have one)
void MainWindow::on_aboutAction_triggered()
{
QMessageBox::about(0, "window title", "<a href='http://www.jeffersonpalheta.com'>jeffersonpalheta.com</a>");
}