Display not updating on the QTimer? - qt

Trying to display text based on when the QTimer fires off...
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_label1 = new QLabel("My Label not working", this);
QTimer* timerDisplay = new QTimer(this);
connect(timerDisplay, SIGNAL(Started()), this, SLOT(updateDisplay(this)));
timerDisplay->start(10);
}
void updateDisplay(MainWindow* m_this)
{
QString out;
out = "hello";
m_this->m_label1->setText("asdf");
}

connect(timerDisplay, SIGNAL(Started()), this, SLOT(updateDisplay(this)));
This statement is failing. And you're ignoring the message that Qt is printing on your console.
The problem is, you can't pass variables in connect statements like that. And what for, by the way? You can use this in the updateDisplay method without the need of passing it in explicitely!

Related

How to add a QAction to a QListWidget

I have the following code:
roslaserscandoialog.h
public:
explicit ROSLaserScanDialog(QWidget *parent = nullptr);
~ROSLaserScanDialog();
QListWidgetItem *createItemFromAction(const QAction* action);
private slots:
void on_listWidget_itemClicked(QListWidgetItem *item);
private:
Ui::ROSLaserScanDialog *ui;
QAction *mAddMsgs;
QAction *mDeleteMsgs;
roslaserscandoialog.cpp
ROSLaserScanDialog::ROSLaserScanDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ROSLaserScanDialog)
{
ui->setupUi(this);
connect(ui->listWidget,SIGNAL(on_listWidget_itemClicked(QListWidgetItem*)),this,SLOT(createItemFromAction(QListWidgetItem*)));
}
QListWidgetItem *ROSLaserScanDialog::createItemFromAction(const QAction *action)
{
Q_ASSERT( action );
QListWidgetItem *mAddMsgs = new QListWidgetItem();
mAddMsgs->setText( action->text() );
mAddMsgs->setToolTip( action->toolTip() );
mAddMsgs->setIcon( action->icon() );
// ...
return mAddMsgs;
}
void ROSLaserScanDialog::on_listWidget_itemClicked(QListWidgetItem *item)
{
mAddMsgs = new QAction(QIcon(":ros.png"), tr("Add New Message"), this);
mDeleteMsgs = new QAction(QIcon(":remove_item.png"), tr("Remove Message"), this);
}
What I have done so far:
I came across this post and also this one. Very useful as I set up my project in a very similar way, but nothing happens when I try to click on the QListWidget.
I know that in order to trigger the action I have to go to the slot called itemClicked as I did on the above code provided.
On the official documentation I was trying to apply what is advised but I don't know why nothing happens.
Please point to the right direction for solving this problem.
Look at console output, there should a warning about connect failing. If you look at your code, the reason should be pretty obvious. Consider
SLOT(createItemFromAction(QListWidgetItem*))
versus your method which isn't even a slot
QListWidgetItem *createItemFromAction(const QAction* action);
See the difference?
And then you have this slot:
void on_listWidget_itemClicked(QListWidgetItem *item);
which you are trying to use as a signal
SIGNAL(on_listWidget_itemClicked(QListWidgetItem*))
That obviously won't work.
It's a bit unclear what you want to happen when an item is clicked, but maybe you just should call createItemFromAction directly from the on_listWidget_itemClicked.
Additionally, add debug print or use breakpoint to verify that the on_listWidget_itemClicked is actually called when you click an item. If not, then you are missing connecting the relevant signal from your list view, ie. ui->setupUi(this); does not have that connect (in other words you did not do the connection in the GUI Designer).

how to use connect with member function

I want to connect the tabBarDoubleClicked signal with a member function but the compiler keeps barking at me with:
/home/ron/src/kterminal/sessionstack.cpp:79:56: error: invalid use of non-static member function
this, SessionStack::editTabLabel(session->id()));
^
and I'm not sure how to fix it, my code looks like:
int SessionStack::addSession(Session::SessionType type)
{
Session* session = new Session(type, this);
connect(session, SIGNAL(titleChanged(int,QString)), this, SIGNAL(titleChanged(int,QString)));
connect(session, SIGNAL(terminalManuallyActivated(Terminal*)), this, SLOT(handleManualTerminalActivation(Terminal*)));
connect(session, SIGNAL(activityDetected(Terminal*)), m_window, SLOT(handleTerminalActivity(Terminal*)));
connect(session, SIGNAL(silenceDetected(Terminal*)), m_window, SLOT(handleTerminalSilence(Terminal*)));
connect(session, SIGNAL(destroyed(int)), this, SLOT(cleanup(int)));
m_sessions.insert(session->id(), session);
QString tab_label = QString("Shell (") + QString::number(session->id(), 16) + ")";
addTab(session->widget(), tr(qPrintable(tab_label)));
emit sessionAdded(session->id());
raiseSession(session->id());
connect(this, tabBarDoubleClicked,
this, SessionStack::editTabLabel(session->id()));
return session->id();
}
void SessionStack::editTabLabel(int tabIndex)
{
How can I get editTabLabel() invoked when the tab is double clicked?
EDIT1
In my header I have the following declared:
class SessionStack : public QTabWidget
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kterminal")
public:
explicit SessionStack(QWidget* parent, QWidget* window);
~SessionStack();
private slots:
void tabBarDoubleClicked(int index);
void editTabLabel(int tabIndex);
};
the error occurs because you just forgot to add SIGNAL and SLOT macros at
connect(this, tabBarDoubleClicked, this, SessionStack::editTabLabel(session->id()));
You can't pass session->id() while connecting, you can only pass the argument when you emit the signal.
Please note that your code is not going to connect anyway in runtime since the signal misses an argument (int), so that it matches the editTabLabel slot. So you need to fix that as well.. should be
connect(this, SINGAL(tabBarDoubleClicked(int), this, SLOT(editTabLabel(int)));
if the signal "tabBarDoubleClicked" is builtin and you can't change it to add an argument then you might consider QSignalMapper http://doc.qt.io/qt-5/qsignalmapper.html

calling quit() method of QApplication

if i try to use quit() method directly, it is compiling perfectly,
however during runtime there comes an error saying "Object::connect: No such slot myClass::quit()."
so to avoid this, is there any way?
by using a method quitPicture()(defined as slot) the application is working fine.
is this the only solution?
myClass::myClass(QWidget *parent)
: QWidget(parent)
{
QWidget *window = new QWidget;
window->setWindowTitle(QObject::tr("Class"));
QPushButton *quitButton = new QPushButton("&Quit");
// QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quit())); //showing run time error
QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(quitPicture())); //working perfectly
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(this);
layout->addWidget(quitButton);
window->setLayout(layout);
window->show();
}
void myClass::quitPicture()
{
std::cout << "calling quitPicture" << std::endl;
QApplication::quit();
}
The button's clicked signal can be connected directly to the application's quit slot:
QObject::connect(quitButton, SIGNAL(clicked()),
QApplication::instance(), SLOT(quit()));
This answer covers new signal/slot syntax in Qt and also additionally covers how to handle it when using a signal that uses overloads.
For signals with no overloads using QObject as an example object:
QObject obj(nullptr);
QObject::connect(&obj, &QObject::destroyed, QCoreApplication::instance(), \
&QCoreApplication::quit);
For signals with overloads using QProcess as an example object:
QProcess * process = new QProcess(QCoreApplication::instance());
QObject::connect(process, static_cast<void (QProcess::*)(int)>(&QProcess::finished), \
QCoreApplication::instance(), &QCoreApplication::quit);
That crazy-looking syntax is basically this, as placeholder syntax:
static_cast< _signalReturnType_( _ObjectName::*_ )( _overloadType1_, _overloadType2_, \
…etc )>( _&ObjectName::signalName_ )
You can check out this link if you want the details on why.
Since QProcess has two overloads, this is the other overload for it:
QProcess * process = new QProcess(QCoreApplication::instance());
QObject::connect(process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>( \
&QProcess::finished ), QCoreApplication::instance(), &QCoreApplication::quit);
If this crazy-looking stuff is spinning your head, don't sweat it. You can comment questions here, as I usually check SO daily, or at least nowadays.
void my_exit_func()
{
// in mainwindow.cpp
delete MainWindow;
}

Work around for signal and slot argument limitation

I want to make a PushButton when it is clicked, its text change into 'clicked'. I tried it by
connect(button1, SIGNAL(clicked()), this, SLOT(markClicked(button1)));
where this refer to the MainWindow and
void MainWindow::markClicked(QPushButton *button) { button->setText("Clicked"); }
It does not seem to work because I think SLOT cannot take more arguments than SIGNAL. If there any approach to work around this limitation?
Thanks.
Qt signals/slots mechanism can only transfer signal to slot function with similar parameters. As a workaround, you should use QSignalMapper:
QSignalMapper mapper;
...
connect(button1, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(button1, button1); // not sure whether this is mandatory or not
...
connect(&mapper, SIGNAL(mapped(QWidget*)), this, SLOT(markClicked(QWidget*)));
and function markClicked is
void MainWindow::markClicked(QWidget *widget) {
QPushButton *button = qobject_cast<QPushButton*>(widget);
button->setText("Clicked");
}
The other way you could do this is to use a default value for the argument and then use the sender() method:
In MainWindow:
void markClicked(QPushButton *button = NULL);
then:
connect(button1, SIGNAL(clicked()), this, SLOT(markClicked()));
and:
void MainWindow::markClicked(QPushButton *button) {
if (button==NULL) { button = qobject_cast<QPushButton*>(sender()); }
button->setText("Clicked");
}

Qt QNetworkAccessManager & multiple QNetworkReplay

I have two http get methods.
First is getting UserID and second is getting full information about current user;
I want to handle finished signlas with different slots
handle GetUserID finished with GetUserIDCompleted and handle GetUserDetails with GetUserDetailsCompleted
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
nam = new QNetworkAccessManager(this);
GetUserID();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserIDCompleted(QNetworkReply*)));
GetUserDetails();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserDetailsCompleted(QNetworkReply*)));
}
does it possible to get QNetworkReplay in different SLOTS?
maybe you can do something like this: having an enum of the different methods
enum GetMethod
{
getUserId,
getUserDetails
};
And you keep a hash of the reply and the corresponding method:
QHash<QNetworkReply*, GetMethod> hash;
QNetworkReply *reply1 = nam->post(requestUserId, data);
hash[reply1] = GetMethod::getUserId;
QNetworkReply *reply2 = nam->post(requestUserDetails, data);
hash[reply2] = GetMethod::getUserDetails;
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
And have one slot that calls the right function
void MainWindow::finished(QNetworkReply *reply)
{
switch(hash[reply])
{
case GetMethod::getUserId:
GetUserIDCompleted(reply);
break;
case GetMethod::getUserDetails:
GetUserDetailsCompleted(reply);
break;
}
hash.remove(reply);
}
I haven't tried it and took some shortcuts but you get the spirit of it =) . It seems that you can retrieve the request with the answer, but I think it is easier with the enum.
Hope it helped
Every operation you do with your QNetworkAccessManager will return a QNetworkReply. This has also has an signal finished. Maybe you can connect this signal to your different slots.
Good luck

Resources