hide QWidget from a different QWidget - c++

I have a MainWindow with two widgets, buttonsWidget and infoWidget.
I'm trying to to hide infoWidget after clicking a button within buttonsWidget (and ultimately show a different widget).
I've tried:
mainwindow.h
public:
void hideInfo();
mainwindow.cpp
void MainWindow::hideInfo()
{
ui->info->hide();
}
buttonsWidget.cpp
void buttonsWidget::on_timingButton_clicked()
{
MainWindow::hideInfo();
//Then will do something to show 'timingWidget'..
}
Many thanks

You should use Signals and Slots for this.
Add a signal in the buttonsWidget.h.
signals:
void hideInfoSignal();
In the main function, connect the button signal with the mainwindow method hideInfo().
QObject::connect(this->info, SIGNAL(hideInfoSignal),this, SLOT(hideInfo));
I haven't tested this, because I dont have Qt on this machine, but that should work, with possible minor modifications. If any errors appear, let me know and I will help. Also, read the signals and slots documentation.

Related

Qt slot and signal: no matching function in MainWindow

I have looked at a variety of the Qt discussions for this error "no matching function for call to" and I still cannot see what is different in this case. I have successfully set up slot/signal pairs between GUI elements, but for some reason the latest set of slot/signal pairs is creating an error.
In order to allow all GUI elements to update the status bar on the main window I have created a signal in each panel as shown here
class PanelA : public QWidget
{
...
public signals:
void UpdateStatusBar(std::string);
...
}
Then in MainWindow there is a slot
//from MainWindow.h
class MainWindow : public QMainWindow
{
private slots:
void ReceiveStatus(std::string);
}
//from MainWindow.cpp
void MainWindow::ReceiveStatus(std::string s)
{
//I can provide other controls, filters, etc.
//but currently there are none
ui->statusBar->showMessage(tr("System status: "+s));
}
And finally, in the MainWindow constructor I have several signals already and I have added one new connect line for each GUI element.
connect(ui->panelA, &PanelA::SelectionChanged, ui->panelB, &PanelB::UpdateSelection);
//this one works
connect(ui->panelA, &PanelA::UpdateStatusBar, ui, &MainWindow::ReceiveStatus);
//this one generates an error there is one status bar connection for each
So, as far as I can tell the syntax is right. both ui->panelA and ui are pointers. I don't know why one is correct and the other is wrong. I'd appreciate any suggestions.
Probably should be:
connect(ui->panelA, &PanelA::UpdateStatusBar, this, &MainWindow::ReceiveStatus);
The ui object isn't a MainWindow, but this will be.

How to add hyperlinks in Qt without QLabel?

I have some labels and layouts nested inside a QWidget to build a part of a sidebar. Each QWidget is its own section and one component currently looks like this:
To my understanding, you can only set hyperlinks with QLabel, but I'm trying to get the whole area between the white lines clickable. This is including the icon and the whitespace. Is there any way to achieve this?
This got marked as a duplicate to the opposite of what I was asking, so I'd like to reiterate that I'm trying to implement a hyperlink without QLabel.
You can easily have a widget open a link on click:
class Link : public QWidget {
Q_OBJECT
public:
Link(QUrl url, QWidget p = nullptr) : QWidget(p), _url(url) {}
QUrl _url;
void mouseReleaseEvent(QMouseEvent *) { QDesktopServices::openUrl(_url); }
}
You can avoid any extra signals and connections, and have each link widget store its own link internally, the url can be set on construction and changed at any time. Not using signals and slots makes it easier to change the link too, without having to disconnect previous connections.
IMO going for a signals and slots solution is only justified when you want different arbitrary behavior. In this case you always want the same - to open a particular link, so you might as well hardcode that and go for an easier and more computationally efficient solution.
I would just manually catch the SIGNAL for clicked() and use desktop services to open the url in code.
bool QDesktopServices::openUrl ( const QUrl & url ) [static]
Opens the given url in the appropriate Web browser for the user's desktop environment, and returns true if successful; otherwise returns false.
http://doc.qt.io/qt-4.8/signalsandslots.html
Using this type of syntax, or in the designer, you can also connect a signal to a slot.
connect(widgetThatRepresentsURL, SIGNAL(clicked()),
handlerThatWillOpenTheURL, SLOT(clicked_on_url()));
For widgets that don't have a signal set up for clicked (or whatever event you are interested in), you can subclass the widget in question and reimplement...
void QWidget::mousePressEvent ( QMouseEvent * event ) [virtual protected]
Specifically for creating a signal, there is emit. I've used this in the past like the following
void Cell::focusInEvent(QFocusEvent *e)
{
emit focus(this, true);
QLineEdit::focusInEvent(e);
}
with the following in the header
signals:
void focus(Cell *, bool);

Qt Minimized Taskbar Icon Mouse Event

I've tried virtually every way possible to hook the activated() signal of a taskbar icon to a corresponding slot. However, I do not understand why Qt(Qt5 Cretor) says I don't have a matching function for the slot.
Qt Error:
C:\Users\potato\Desktop\CCT-master\CCTracker\cctsystemtray.cpp:40:
error: no matching function for call to
'QObject::connect(QSystemTrayIcon*&, const char*, CCTSystemTray*
const, const char*)'this,
SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)))
As far as I can tell, according to Qt Documentation,
The activated() signal is used to catch mouse events such as clicks and double clicks for the taskbar icon. At first try I thought it's only signals and slots, but then for whatever reason activated() just won't fit. And If I try connecting activated() to a slot from another class, everything fails due to pointer errors.
Here's are the working parts inside CCTSystemTray.h:
class CCTSystemTray
{
public:
CCTSystemTray();
void initSystemTray(QWidget *rootWindow);
private:
QSystemTrayIcon* systray;
public slots:
void systrayActivated(QSystemTrayIcon::ActivationReason);
};
And CCTSystemTray.cpp:
// Above are Class declearations, menu items, etc...
QObject::connect(this->systray, SIGNAL(QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason)),
this, SLOT(systrayActivated(QSystemTrayIcon::ActivationReason)));
}
void CCTSystemTray::systrayActivated(QSystemTrayIcon::ActivationReason)
{
qDebug() << "Hello";
}
The full code sample can be found here on pastebin.
If someone can help me out from hair-pulling, that'd be great!
Thank you all.
Your class CCTSystemTray has a slot, but it is not a QObject (it doesn't derive from QObject or another class derived from QObject) and also you need the Q_OBJECT macro in your class declaration and then signals and slots will work.

QT Creator Main window - how to change the interface for each element from the menu?

I am new to QT Creator. I did create a menu: Login || Open. When login is clicked I would like to see a line edit and a press button. When Open is clicked I would like to see a picture in the window. Can I change the interface of the same window depending on what I click in the menu bar? How can I do that?
I did something similar to this - an app with several major areas, toggled by an icon bar at the top.
I used a QStackWidget to stack the different application areas on top of each other, a set of QActions that I created using the designer, and a QActionGroup to implement the toggling.
When the actions are marked as "checkable" and grouped in a QActionGroup, theQToolBar only lets one be active at the time.
Here's a simplified extract of my code:
// MyApp.h
#include <QMainWindow>
class QAction;
class QActionGroup;
namespace Ui {
class MyApp;
}
class MyApp: public QMainWindow
{
Q_OBJECT
public:
explicit MyApp(QWidget *parent = 0);
~MyApp();
public slots:
void showSection(QAction* a);
private:
Ui::MyApp *ui;
QActionGroup* sections;
};
//MyApp.cpp
#include "structureapp.h"
#include "ui_structureapp.h"
#include <QActionGroup>
MyApp::MyApp(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyApp),
sections(new QActionGroup(this)),
{
ui->setupUi(this);
/* Populate section list */
/* Page indices for the stack widget*/
ui->actionSectionOne-> setData(0);
ui->actionSectionTwo-> setData(1);
ui->actionSectionThree-> setData(2);
sections->addAction(ui->actionSectionOne);
sections->addAction(ui->actionSectionTwo);
sections->addAction(ui->actionSectionThree);
ui->mainToolBar->addSeparator();
connect(sections, SIGNAL(triggered(QAction*)), this, SLOT(showSection(QAction*)));
/* Show the default section */
ui->actionContentSection->trigger();
}
MyApp::~MyApp()
{
delete ui;
}
void MyApp::showSection(QAction *a)
{
ui->mainArea->setCurrentIndex(a->data().toInt());
}
Yes, you can. As I explained earlier, each menu entry is a signal, and that's connected to a slot. With two different menu entries, you have two signals, and you'd connect them to two different slots. So, you could name your first slot onLogin, and the second slot onOpen. (It helps to choose descriptive names, so you'll understand your program when you come back on mondays).
Now, it the slot onLogin, you put the code for login. In the slot onOpen, you put the other code. But do consider what happens if you click the two menu entries one after another. Should that even be possible? If not, you may need another solution. It's quite common to use a QDialog for a login. When a dialog is active, you can't use the menu of the main application, so you can't accidentily hit onOpen when you're busy with the login.

can't connect QT signal

i am new to QT and was trying to create a custom signal that would tell me a removable disk has been inserted.
This is what i did
MainWindow.h
class MainWindow
{
QOBJECT
..
..
signals:
void qm_diskInserted(QString &);
public slots:
void addItemToList(QString &);
...
}
MainWindow.cpp
void MainWindow::onDeviceChange(MSG * msg)
{
//code for detecting device here
QString &driveLetter= getDriveLetter(mask);
//try to emit QT signal here
emit qm_diskInserted(driveLetter);
}
MainWindow::MainWindow(QWidget * parent=NULL)
{
ui.setupUi(this);
QObject::connect(this, SIGNAL(qm_diskInserted(QString&)), this, SLOT(addItemToList(QString &));
}
void MainWindow::addItemToList(QString &)
{
//more stuff here
}
somehow the slot addItemToList() isn't called and i have to call it manually.
What am i doing wrong?
Thanks.
PS:
By the way is there any way of debugging signals?
Ie how can i be sure that a signal is emitted?
It is at least supposed to be Q_OBJECT. I think you also need to inherit QMainWindow.
This is a long shot, but are you sure onDeviceChange() method is called?
EDIT
Classes which have Q_OBJECT macro in their body needs to inherit directly or indirectly from QObject, and in your code it is not the case.
With connection problems, always make sure that you check the console for messages about connect failures. Since Qt can't tell if a connection makes sense until runtime, it notifies you of failures there. You'd think it would crash, but it just quietly says these things in the console.
With Qt, it makes sense to watch the console always. Qt prints out all sorts of error messages that can help when you've got a problem.
Try making your signals virtual void instead and make sure that your MainWindow class inherits (directly or indirectly) from QObject
EDIT
As mentioned in other comments, the macro should be Q_OBJECT