I'm having a QWebView in my windows that contains text. I want the selected word (or words it doesn't matter) to be written somewhere else, but the signal selectionChanged is never called. Do I need to set something before? For now, all I have is a slot waiting be called...I don't even do something for now because the signal is never sent
thanks!
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
this->setupUi();
connect(m_WebView, SIGNAL(selectionChanged()), this, SLOT(newSelection()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupUi()
{
m_WebView = new QWebView();
m_LineEdit = new QLineEdit();
QVBoxLayout* lay = new QVBoxLayout();
lay->addWidget(m_LineEdit);
lay->addWidget(m_WebView);
QWidget* wid = new QWidget(this);
wid->setLayout(lay);
setCentralWidget(wid);
}
void MainWindow::newSelection()
{
m_LineEdit->setText(m_WebView->selectedText());
}
I'm not sure why the example you gave didn't work. This works (and it is because I am connection to the page not the view). I'd actually consider filing a bug report with the devs if I were you. From the docs, it seems like this SHOULD work.
cpp:
#include "mainwindow.h"
#include <QWebView>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
this->setupUi();
connect(m_WebView->page(), SIGNAL(selectionChanged()), this, SLOT(newSelection()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::setupUi()
{
m_WebView = new QWebView();
m_WebView->load(QUrl("http://qt.nokia.com/"));
m_WebView->show();
m_LineEdit = new QLineEdit();
QVBoxLayout* lay = new QVBoxLayout();
lay->addWidget(m_LineEdit);
lay->addWidget(m_WebView);
QWidget* wid = new QWidget(this);
wid->setLayout(lay);
setCentralWidget(wid);
}
void MainWindow::newSelection()
{
m_LineEdit->setText(m_WebView->selectedText());
}
h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QWebView;
class QLineEdit;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setupUi();
protected slots:
void newSelection();
private:
Ui::MainWindow *ui;
QWebView * m_WebView;
QLineEdit * m_LineEdit;
};
#endif // MAINWINDOW_H