QT error in connecting passing a QString element - qt

I was following some code posted in other questions on how to connect the status bar between parent and distant child by means of signals and slots.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
...
connect(myCanvas,SIGNAL(mouseMoved(QString)),
this,slot(setStatusBar(QString)));
...
}
Somewhere in the class MainWindow the following is declared as public slot:
void MainWindow::setStatusBarText(const QString& text) {
statusBar()->showMessage(text);
}
in the class to which myCanvas belongs declares the signal:
signals:
void mouseMoved(const QString&);
And emits the signal:
void GraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent)
{
...
emit mouseMoved(QString("("+QString::number(mouseMoveCatch.x())+";"
+QString::number(mouseMoveCatch.y())+")"));
...
}
I am sure QString is properly included. But when i compile I get an error on the connect line saying "QString does not refer to a value".
I have no clue what this means. Thanks for any help!

You have a case error on the line
connect(myCanvas,SIGNAL(mouseMoved(QString)),
this,slot(setStatusBar(QString)));
You should be using SLOT, not slot.

Related

Slot function is not called by currentChanged signal of treeView

I want to call a function indexChanged() if an index in my treeView is changed.
I used ui->treeView->currentChanged() signal but it didn't call indexChanged() slot, even though I connected the signal to the slot.
Here's my code :
.cpp file
TipManager::TipManager(QWidget *parent) :
QWidget(parent),
ui(new Ui::TipManager)
{
ui->setupUi(this);
connect(ui->treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &TipManager::indexChanged);
...
}
void TipManager::indexChanged(const QModelIndex &current, const QModelIndex &previous)
{
trimCurrentPath(previous);
}
.h file
namespace Ui {
class TipManager;
}
class TipManager : public QWidget
{
Q_OBJECT
public:
explicit TipManager(QWidget *parent = 0);
~TipManager();
public slots:
void indexChanged(const QModelIndex &current, const QModelIndex &previous);
private:
Ui::TipManager *ui;
...
};
I also tested in debug mode, but the slot function indexChanged() is not even called. Plus, it shows this message instead : QObject::connect: invalid null parameter
I hit a similar issue (the slot wasn't being triggered). Following #G.M.'s pointer in the comments to your question, it turned out that the call to ui->treeView->selectionModel() returns null if there's no model yet (and setSelectionModel() hasn't yet been called, presumably).
If I populated my QTreeView prior to calling ui->treeView->selectionModel() within the connect() call then I received my non-null response and the slot was triggered by the signal as desired.

Qt and "No such slot" error

I wrote the class and add a slot:
class graphShow : public QObject {
Q_OBJECT
public:
graphShow(){}
public slots:
void upd(QGraphicsScene &S);
};
Implementation of graphShow::upd is here:
void graphShow::upd(QGraphicsScene &S) {
QGraphicsTextItem* pTextItem = S.addText("Test");
pTextItem->setFlags(QGraphicsItem::ItemIsMovable);
}
Connection:
graphShow gr;
QPushButton* p1 = new QPushButton("Show");
/*...*/
QObject::connect(p1,SIGNAL(clicked()),&gr,SLOT(upd(&scene);));
During compiling I have no errors but when program starts I see this message:
Object::connect: No such slot graphShow::upd(&scene); in main.cpp:93
What am I doing wrong?
You need to set up connection in the following way:
QObject::connect(p1, SIGNAL(clicked()), &gr, SLOT(upd(QGraphicsScene &)));
However this also may not wark, because Qt docs state:
The signature of a signal must match the signature of the receiving
slot. (In fact a slot may have a shorter signature than the signal it
receives because it can ignore extra arguments.)
By the way, you doing it wrong. You could not connect signal without arguments to slot with argument. For your case you should use QSignalMapper.

javaScriptWindowObjectCleared signal is not invoked while html page loaded in qwebview

I am new to QT. I have been trying to see the bridge between javascript and Qt Class.
What I did:
1) I have a button and connected clicked signal to on_pushButton_clicked, also I have qwebview instance.
In on_pushButton_clicked:
...
QUrl url = QUrl::fromLocalFile("C:\\whoami\\sd\\index.html");
QObject::connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(addJSObject()));
ui->webView->page()->mainFrame()->load(QUrl(url));
....
and in addJSObject,
void MainWindow::addJSObject()
{
qDebug () <<"Inside addJSObject";
ui->webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("mBridge"), m_bridge);
qDebug () <<"Nooo. I m not invoked..";
}
Problem:
It compiles without error, and html file is displayed in the qwebview, but addJSObject callback is not invoked.
Could someone help me in order to resolve this issue?.. I must have done some silly mistake. :(.
Make sure, that your MainWindow: 1) inherits QObject 2) has Q_OBJECT macro 3) has a slot addJSObject().
For example mainwindow.h:
class MainWindow : public QObject
{
Q_OBJECT
public:
MainWindow();
private slots:
void addJSObject();
};
If this is correct, you should be able to connect to addJSObject() without static QObject::connect(). Just use connect() when referring to this as signal target object.
One way to just check, that your signalling works is to try using QTimer timeout signal:
QTimer::singleShot(5000, this, SLOT(addJSObject()));

Qt matching signal with custom slot

I'm trying to use a QAction (QMenu member entry) to open a new window. Precisely: I want actionAbout signal predefined activated to match MainWindow custom slot open AboutWindow - and that's what I've got trouble with.
I know that I can use either the connect Qt function manually inside the source main_window.cpp file or just click it up in the Qt Creator, but my custom slot doesn't show up so I cannot select it. Maybe my slot function declaration is wrong (invalid parameters) and that's why QtCreator doesn't allow me to choose my custom slot in the GUI signals & slots. Could anyone point me what should I do to make QtCreator display my custom slot in the dropdown and how should the connect function call look like?
This is my main_window.h file content:
#include
#include "about_window.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void openAboutWindow();
private:
Ui::MainWindow *ui;
Ui::AboutWindow *aboutWindow;
};
And this is main_window.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openAboutWindow(QWidget *parent)
{
aboutWindow = new Ui::AboutWindow(parent); // Be sure to destroy you window somewhere
aboutWindow->show();
}
The compiler shouts about both constructor and openAbutWindow:
../Application/main_window.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
../Application/main_window.cpp:9:13: error: ‘actionAbout’ was not declared in this scope
../Application/main_window.cpp:9:80: error: expected ‘)’ before ‘;’ token
../Application/main_window.cpp: In member function ‘void MainWindow::openAboutWindow(QWidget*)’:
../Application/main_window.cpp:19:44: error: invalid use of incomplete type ‘struct Ui::AboutWindow’
../Application/about_window.h:7:11: error: forward declaration of ‘struct Ui::AboutWindow’
../Application/main_window.cpp:20:15: error: invalid use of incomplete type ‘struct Ui::AboutWindow’
../Application/about_window.h:7:11: error: forward declaration of ‘struct Ui::AboutWindow’
../Application/main_window.cpp:9:13: error: ‘actionAbout’ was not declared in this scope
The error message says it all, where is the QAction defined? Should it be ui->actionAbout?
connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this));
openAboutWindow() does not take any arguments, and regardless this is an instance not a type.

QT signals and slots unexpected Error

This is driving me insane....it was working earlier, but not it doesn't work. I have defined Q_SLOTS and Q_SIGNALS, and I was trying to connect them. It was working to an extent...and then all of a sudden everything stopped working, and now I am getting errors. My code is the following:
ControllerLogic.h
#ifndef CONTROLLERLOGIC_H
#define CONTROLLERLOGIC_H
#include "initdataaccess.h"
#include "mainframe.h"
#include <QtGui>
#include "initializationdatastructure.h"
/** This is a controller class; refering to the model-view-controller
* architecture.
*/
class ControllerLogic : public QObject
{
Q_OBJECT
public:
ControllerLogic(InitDataAccess *initDataAccess, MainFrame *mainFrame);
Q_SIGNALS:
void Signal();
private:
void setMainFrame(MainFrame mainFrame);
public Q_SLOTS:
void receive();
};
#endif // CONTROLLERLOGIC_H
ControllerLogic.cpp
#include "controllerlogic.h"
#include "licensedataaccess.h"
#include <qobjectdefs.h>
// obsolete...may be used later
ControllerLogic::ControllerLogic(InitDataAccess *initDataAccess, MainFrame *mainFrame)
{
connect(this, SIGNAL(signal()), mainFrame, SLOT(PrintTestSlot()));
}
void ControllerLogic::receive(){
qDebug()<<"RECEIVE";
}
void ControllerLogic::Signal(){
qDebug()<<"SIGNAL";
}
ERROR
moc_controllerlogic.obj:-1: error: LNK2005: "protected: void __thiscall ControllerLogic::Signal(void)" (?Signal#ControllerLogic##IAEXXZ) already defined in controllerlogic.obj
release\TSLSuite.exe:-1: error: LNK1169: one or more multiply defined symbols found
I also tried to define the signal as follows:
public:
Q_SIGNAL void Signal();
but I get the same error.
What is going on?
Please Help!
Thanks!
The problem is that you're trying to define a function called Signal()
Qt generates the body of the "signal" functions for you, and if you try to create your own definition, you will get the error that you're describing.
(As a side note, your connect statement appears to be broken s/signal/Signal/)

Resources