How to use QWebEnginePage::OpenLinkInNewTab [Qt5.8] - c++

When I click on link to any question in my feed on Quora using this code, the link doesn't open but it doesn't print "Hello". Could you please tell me where am I wrong? I'm pretty sure that link on quora emits the OpenLinkInNewTab signal. Please help, thanks.
class WebView : public QObject {
void newTabRequested() {
std::cout<<"Hello"<<std::endl;
}
public:
char* home_page;
QAction* newTabAction=new QAction();
QWebEngineView* view=new QWebEngineView();
WebView(char* page=(char*)"https://google.com") {
this->home_page=page;
this->exitFullScreen->setShortcut(Qt::Key_Escape);
createWebView();
this->view->settings()
->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows,true);
this->newTabAction=this->view->pageAction(QWebEnginePage::OpenLinkInNewTab);
connect(this->newTabAction,&QAction::toggled,this,&WebView::newTabRequested);
}
void createWebView() {
this->view->load(QUrl(this->home_page));
}
};

I think the problem is that newTabRequested is not a slot. Try
class WebView : public QObject{
Q_OBJECT
private slots:
void newTabRequested(){
std::cout<<"Hello"<<std::endl;
}
// ...
}

Related

QML pointer property change signal not propagated to binding properties

I exposed a pointer variable to qml like this:
Fruit:
class Fruit : public QObject
{
Q_OBJECT
Q_PROPERTY(int qualityGrade READ qualityGrade WRITE setQualityGrade NOTIFY qualityGradeChanged)
Q_PROPERTY(bool organic READ organic WRITE setOrganic NOTIFY organicChanged)
public:
int qualityGrade() const
{
return m_qualityGrade;
}
bool organic() const
{
return m_organic;
}
public slots:
void setQualityGrade(int qualityGrade)
{
if (m_qualityGrade == qualityGrade)
return;
m_qualityGrade = qualityGrade;
emit qualityGradeChanged(m_qualityGrade);
}
void setOrganic(bool organic)
{
if (m_organic == organic)
return;
m_organic = organic;
emit organicChanged(m_organic);
}
signals:
void qualityGradeChanged(int qualityGrade);
void organicChanged(bool organic);
private:
int m_qualityGrade = -1;
bool m_organic = false;
};
MyClass.h:
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(Fruit* featuredFruit READ featuredFruit WRITE setFeaturedFruit NOTIFY featuredFruitChanged)
public:
explicit MyClass(QObject *parent = nullptr);
~MyClass();
Fruit* featuredFruit() const
{
return m_featuredFruit;
}
public slots:
void setFeaturedFruit(Fruit* featuredFruit)
{
if (m_featuredFruit == featuredFruit)
return;
m_featuredFruit = featuredFruit;
emit featuredFruitChanged(m_featuredFruit);
}
signals:
void featuredFruitChanged(Fruit* featuredFruit);
private:
Fruit* m_featuredFruit = nullptr;
};
MyClass.cpp:
MyClass::MyClass(QObject *parent) : QObject(parent)
{
m_featuredFruit = new Fruit();
m_featuredFruit->setQualityGrade(2);
QTimer *timer = new QTimer();
connect(timer, &QTimer::timeout, this, [=]() {
//m_featuredFruit->deleteLater(); //<--- activating these two lines causes to force working featuredFruitChanged signal
//m_featuredFruit = new Fruit();
m_featuredFruit->setQualityGrade(5);
emit featuredFruitChanged(m_featuredFruit);
delete timer;
});
timer->start(5000);
}
MyClass::~MyClass()
{
m_featuredFruit->deleteLater();
m_featuredFruit = nullptr;
}
and I used it in QML as follow:
MyClass {
id: classObj
onFeaturedFruitChanged: console.log("original property shows an change");//<--- called as expected
}
Item {
property Fruit selectedFruit: classObj.featuredFruit //<--- binding qml defined property to C++ property
onSelectedFruitChanged: {
console.log("binded property recieved change signal");//<--- not called after changes!!!
alertAnimation.restart(); //<--- an example of usage
}
}
The problem is whenever I emit featuredFruitChanged, the binding qml property does not received change signal.
What is wrong?! Is this a Qt Framework bug? Any suggestion?
Also I tried overloading equality operator in C++ without success
Update:
OK, I add some more precisions to my sample code in order to reproduce problem easier.
A typo in my sample code fixed (thanks #ihor-drachuk). The problem exist yet.
Because you misspelled featuredFruit and wrote featuedFruit. Fix it and it will work.
Update: It should work if call setFeaturedFruit or if change it from QML. It will not work as you expect if change some property of featuredFruit even if it is selected
Update 2: QML call onChanged if changed value, value is pointer to object. So if pointer changed - then onChanged will be called. If something behind pointer changed - no.
But you can handle it with help of Connections:
Connections {
target: classObj.featuredFruit
onTargetChanged: console.warn("Target changed!");
onQualityGradeChanged: console.warn("onQualityGradeChanged!");
onOrganicChanged: console.warn("onOrganicChanged!");
}
Also you can add to Fruit some special signal like somethingChangedInMe and emit it in each setter. So, you can write in Connections just this signal handler:
Connections {
target: classObj.featuredFruit
onTargetChanged: console.warn("Target changed!");
onSomethingChangedInMe: console.warn("onSomethingChangedInMe!");
}

QT open a form(.ui) from main form by button push

I have searched many forums to solve this problem so please don't just link a forum.
I have my main.ui that is loaded at the start. it's header
using namespace Ui;
class main: public QMainWindow
{
Q_OBJECT
public slots:
void AddPlayer();
public:
main(QWidget *parent = 0);
~main();
private:
Ui::mainClass ui;
Ui::FormPla uiPlayer;//the name of the form i want to open
};
Then i have the main.cpp
main::main(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.mainBtnPla, SIGNAL(clicked()), SLOT(AddPlayer()));
}
main::~main()
{
}
void FinalYearProject::AddPlayer()
{
//code to open player.ui
//formPla *obj;
//obj = new formPla();
//obj->show(); //i tried this but show wasnt a member of ui::player
}
player.ui doesnt have a .h or .cpp. I dont know if this affects this. I just want to be able open the form.
ui generated classes cannot be used without a QWidget class loading them.
You must create a separate QWidget base class (Player), loading Ui::FormPla and later create an instance of this class to show the Ui::FormPla on screen.

How to close main window from another class?

I have a class which inherits from QDialog (dialog_game_over). I show an object of that class in a slot of another class. I want to close mainwindow and QDialog when user clicked on ok button. First I built an object of mainwindow in dialog_game_over and close it. But this way it wasn't correct. What do I do for closing program in class other than main class?
dilog_game_over::dilog_game_over(QWidget *parent) :
QDialog(parent),x_size(400),y_size(400)
{
ok=new QPushButton(this);
ok->setText("OK");
ok->move(200,200);
connect(ok,SIGNAL(clicked()),this,SLOT(on_ok_clicked()));
}
void dilog_game_over::on_ok_clicked()
{
accept();
this->close();
}
class Myenemy1 : public QGraphicsObject
{
Q_OBJECT
public slots:
void loss();
private:
dilog_game_over dlg;
}
void Myenemy1::loss()
{
....
dlg.exec();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),size_of_plane_y(600),size_of_plane_x(2500)
{
set_scene();
put_cloud();
put_point();
}
All over your application you can exit by :
qApp->quit();
Or
qApp->exit(0);

Call the base class implementation

I was able to simulate the Right-Click event by subclassing the QTableWidget:
header file:
#ifndef QRIGHCLICKTABLE_H
#define QRIGHCLICKTABLE_H
#include <QTableWidget>
#include <QMouseEvent>
class QRightClickTable : public QTableWidget
{
Q_OBJECT
public:
explicit QRightClickTable(QWidget *parent = 0);
private slots:
void mousePressEvent(QMouseEvent *e);
signals:
void rightClicked();
public slots:
};
#endif // QRIGHCLICKTABLE_H
cpp file
QRightClickTable::QRightClickTable(QWidget *parent) :
QPushButton(parent)
{
}
void QRightClickTable::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::RightButton)
emit rightClicked();
}
QRightClickTable *button = new QRightClickTable(this);
ui->gridLayout->addWidget(button);
connect(button, SIGNAL(rightClicked()), this, SLOT(onRightClicked()));
void MainWindow::onRightClicked()
{
qDebug() << "User right clicked me";
}
Now, right-click works correctly, but there are other problems with QTableWidget: all other mouse events, such as the left click to select a cell, no longer work.
Can you help me? I know I need to call the base class implementation in my override of mousePressEvent, you could show me how with a little piece of code?
Change your event handler like this :
void QRightClickTable::mousePressEvent(QMouseEvent *e) {
if(e->button()==Qt::RightButton) {
emit rightClicked();
} else {
QTableWidget::mousePressEvent(e);
}
}

Adding new functionality to a class

I'm studying the Qt4 library and I want to add some functionality to all the children of QWidget in my project. I need all widgets to have mousePressEvent overridden. Obviously I do not need to override it in every particular widget I use(I use many of them and I they to follow DRY rule) , instead I create a Foo class:
class Foo : public QWidget
{
Q_OBJECT
protected:
/* implementation is just a test */
virtual void mousePressEvent(QMouseEvent*) { this->move(0,0); }
};
And derive my button from it:
class FooButton : public QPushButton , public Foo
{
public:
FooButton (QWidget* parent) : QPushButton(parent) { };
};
But the event seems to be not overridden... What am I doing wrong?
Thanks in advance.
For the mousePressEvent access, try QObject::installEventFilter().
http://doc.qt.digia.com/stable/qobject.html
You are inheriting twice now from QWidget. This is problematic (see diamond problem).
Instead, drop your Foo class and move your custom mouse press event handler to your FooButton class:
class FooButton : public QPushButton
{
Q_OBJECT
protected:
/* implementation is just a test */
virtual void mousePressEvent(QMouseEvent*) { this->move(0,0); }
public:
FooButton (QWidget* parent) : QPushButton(parent) { };
};
If this doesn't suit the design of your application, then try using virtual inheritance instead. That is, change Foo to:
class Foo : public virtual QWidget
If that doesn't help, you should follow the advice of the other answers and install an event handler.
(You can read about virtual inheritance in its Wikipedia article.)
I suspect that the problem is that you're inheriting from QWidget twice, once through QPushButton and once through Foo.
From the way you phrased the question, I'm assuming that you want to do this for varying kinds of widgets, and thus don't want to have to subclass QPushButton, QLabel, QCheckBox, etc. If this is not the case then you should use Nikos's answer.
If not, your best bet is probably doing to be to use an event filter.
class MousePressFilter : public QObject {
Q_OBJECT
public:
MousePressFilter(QObject *parent) : QObject(parent) { }
protected:
bool eventFilter(QObject *watched, QEvent *event) {
QWidget *widget = dynamic_cast<QWidget*>(watched);
widget->move(0,0);
return false;
}
};
And then in your Foo class constructor:
class Foo {
Foo() {
installEventFilter( new MousePressFilter(this) );
}
};