Dynamically add QWebEngineView to layout - c++

I'm trying to dynamically QWebEngineView to already existing layout.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView view;
view.setUrl(QUrl(QStringLiteral("http://www.qt.io")));
view.resize(1024, 750);
view.show();
ui->splitter->addWidget(view);
}
When running this i'm getting error: C2664: 'void QSplitter::addWidget(QWidget *)': cannot convert argument 1 from 'QWebEngineView' to 'QWidget *'
I'm trying to create program for previewing and editing html/text/image files in local file system. This means that i need to switch widget in main window for different tasks. In my designer form I have splitter layout in which I'm trying to add QWebEngineView.
I tried default examples from Qt Designer for WebEngine and WebKit. They work just as planned but instead of using UI layout they are using only code for adding and managing widgets. I want to use form layouts, which means that this option is not suited for me.
How i can fix this issue ?
Is this viable solution for what i'm trying to achieve or there is a better one ?

Try this way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebEngineView *view;
view = new QWebEngineView(this);
view->setUrl(QUrl(QStringLiteral("http://www.qt.io")));
view->resize(1024, 750);
view->show();
ui->splitter->addWidget(view);
}

Related

Qt: Attempting to initialize a layout in mainwindow with mainwindow causes error when given 'this' as parent?

So I'm trying to understand why this output would occur when initializing a layout in the basic mainwindow generated in qt.
QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout
The code that causes this is:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, topHLayout(new QHBoxLayout)
, middleHlayout(new QHBoxLayout(this))
topHlayout causes no error but am not specifying a parent however middleHlayout does when giving the mainWindow as its parent. From my assumption I can't add a layout to a widget? This makes it confusing ass to how would one create new windows with layouts in them. Thanks for any help!

QtListWidgetItem with Pixmap crashes if to many

I'm a noob, so sorry if my question feels dumb.
I use Qt Creator to make a kind of image viewer.
I added a QListWidget and added items with a pixmap. So far, so good.
Now I try to read the hole directory and add all 438 images.
The app crashes with this message:
Cn::Process::NotifyOutOfMemory(). 17:47:36: The program has
unexpectedly finished. 17:47:36: The process was ended forcefully.
If I reduce the count to 85. The app opens, but does only show 77 images.
I tried to fix this by changing addItem to addItems but don't know how to get the QListWidgetItem in a QList or on any other way. And than it is the question of this is a solution.
Can someone give me a kick in the right direction?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDir dir("C:/");
QStringList items; // String???
foreach(QFileInfo var, dir.entryInfoList ()){
if(var.isFile ()){
//items += // What to do here ??
ui->listWidget->addItem (new QListWidgetItem(QPixmap(var.absoluteFilePath ()), var.fileName ()));
}
ui->listWidget->addItems (items);
}
}
Michael

Create QAction with shortcut, without inserting in menu

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <cassert>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QAction* back = new QAction(this);
back->setVisible(true);
back->setShortcut(QKeySequence("Ctrl+M"));
bool cres = connect(back, SIGNAL(triggered(bool)), this, SLOT(mySlot()));
assert(cres);
}
In this code I tried to catch Ctrl+M key event. I don't want to put the action in menu. connect returns true but mySlot is never called. When action is inserted in menu, shortcut works well. What I have done wrong?
QAction is dormant until you insert it somewhere. As vahancho has suggested, use QShortcut. You need to instantiate the shortcut for each top-level widget (window) where you want it to be active. Thus if you have 5 top-level windows, you'll need 5 shortcuts, each having one of windows as its parent.
There is no way to use QShortcut as a global shortcut without the gui. QShortcut is only active when its associated widget has focus. The widget could be a top-level window.
System-global shortcuts are the subject of this question.

How to download files from QWebView?

I created a small web browser with QT Creator and QWebView. I's working very good and the pages are loading very fast. But how can I make my browser capable of downloading files? I looked through the signals and functions list, but I did't find something that could help me.
How can I found out if a QUrl contains a link to a file other than text/html so I can download it?
QWebView has a 'QWebPage' member which you can access it's pointer with webView.page() . This is where you should look. QWebPage has two signals: downloadRequested(..) and unsupportedContent(..). I believe dowloadRequest is only emitted when user right clicks a link and selects 'Save Link' and unsupportedContent is emitted when target URL cannot be shown (not an html/text).
But for unsupportedContent to be emitted, you should set forwardUnsupportedContent to True with function webPage.setForwardUnsupportedContent(true). Here is a minimal example I have created:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webView->page()->setForwardUnsupportedContent(true);
connect(ui->webView->page(),SIGNAL(downloadRequested(QNetworkRequest)),this,SLOT(download(QNetworkRequest)));
connect(ui->webView->page(),SIGNAL(unsupportedContent(QNetworkReply*)),this,SLOT(unsupportedContent(QNetworkReply*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::download(const QNetworkRequest &request){
qDebug()<<"Download Requested: "<<request.url();
}
void MainWindow::unsupportedContent(QNetworkReply * reply){
qDebug()<<"Unsupported Content: "<<reply->url();
}
Remember, MainWindow::download(..) and MainWindow::unsupportedContent(..) are SLOTs !

Qt QLabel default text

My code is very simple:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
lineEdit = new QLineEdit();
label = new QLabel("");
connect(lineEdit, SIGNAL(textChanged(QString)), label, SLOT(setText(QString)));
ui->setupUi(this);
}
I compiled it, and no error or warning.
But When I run it, The UI like this:
Why QLabel's default text was TextLabel?
You should read some tutorials from Qt docs. You're mixing QtDesigner ui with manual widget creation. Your default text on label comes from your ui file. Also you don't need to create your labels/line edits when you use ui file. Just get them stright from ui class. So if you'll get your ui file back to normal, then you may do something like this:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->lineEdit, SIGNAL(textChanged(QString)), ui->label, SLOT(setText(QString)));
}
Also change text in your label with Qt Designer by doubleclick on it.
That's because both your
lineEdit = new QLineEdit();
label = new QLabel("");
are different that the ones you created in your ui. You are defining two new widgets, while you should probably reference the previous ones:
ui->lineEdit->clear();
ui->label->clear();
connect(ui->line....
//etc...