I'm trying to make some project with QTabWidget (a litle browser and a text editor with multiple tab like notepad++) but I'm stuck in 2 project when I try to edit a value of widget (QWebEngine or QTextEdit) inside of QTabWidget. This is the code for the litle browser project:
fp.h :
#ifndef FP_H
#define FP_H
#include <QMainWindow>
#include <QWebEngineView>
#include <QWidget>
#include <QLabel>
QT_BEGIN_NAMESPACE
namespace Ui { class fp; }
QT_END_NAMESPACE
class fp : public QMainWindow
{
Q_OBJECT
public:
fp(QWidget *parent = nullptr);
~fp();
public slots:
void newtab();
void deltab();
void newpage();
QWebEngineView *ap();
int cui();
private:
Ui::fp *ui;
QWebEngineView *webnav;
};
#endif // FP_H
fp.cpp
#include "fp.h"
#include "ui_fp.h"
fp::fp(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::fp)
{
ui->setupUi(this);
ui->in_url->setText("https://google.com");
}
fp::~fp()
{
delete ui;
}
void fp::newtab()
{
QWebEngineView *webnav = new QWebEngineView;
webnav->load(QUrl("https://google.com"));
webnav->setObjectName("webnav");
ui->in_url->setText("https://google.com");
ui->onglet->addTab(webnav,"Home");
}
int fp::cui()
{
return ui->onglet->currentIndex();
}
QWebEngineView *fp::ap()
{
return ui->onglet->currentWidget()->findChild<QWebEngineView *>("webnav");
}
void fp::deltab()
{
ui->onglet->removeTab(cui());
}
void fp::newpage()
{
QString use_url = ui->in_url->text();
ap()->load(use_url);
}
and there what look like the .ui Image of .ui in Qdesigner
I try to make work the method "ap" it should return the QWebEngineView child of current viewed tab but when I call the slots "newpage" (it currently using "ap" method) it just crash the application. This slot is trigered when I enter a new URL in the QLineEdit in .ui (before that I create a new tab whit a QWebEngineView inside whit the slot "newtab" )
void fp::newtab()
{
QWebEngineView *webnav = new QWebEngineView;
webnav->load(QUrl("https://google.com"));
webnav->setObjectName("webnav");
ui->in_url->setText("https://google.com");
ui->onglet->addTab(webnav,"Home");
}
So what wrong whit that line it the good method for get child widget of a QTabWidget? If it the good method what should be modified to make it work?
QWebEngineView *fp::ap()
{
return ui->onglet->currentWidget()->findChild<QWebEngineView *>("webnav");
}
Try something like:
QWebEngineView *view = qobject_cast<QWebEngineView *>(
this->ui->onglet->widget(0)
);
Note to put above somewhere in
fp
's methods (where you need the reference).I could use
ui->onglet->currentWidget()
, but the difference is, that will not work once you have multiple tabs.