Qt Creator no member named stackedWidget - c++

I've just designed my ui in the QT-Creator and, as the main application is based on two panels, I decided to use the StackedWidget for "change the layout" without opening a new window.
So, I added a QTStackedWidget named: stackedWidget (as default).
The problem is in mainwindow.cpp, I added a custom SLOT that contain:
ui->stackedWidget->setCurrentIndex(1);
when I build this the compiler says:
mainwindow.cpp:25: error: no member named 'stackedWidget' in 'Ui::MainWindow'
ui->stackedWidget->setCurrentIndex(1);
~~ ^
also in the qt-creator itself I was unable to attach a signal to the stackedWidget because it doesn't show to me the setCurrentIndex SLOT...
any advice?
Please note that I'm a noob with C++ I just used Qt a couple of years ago with PyQt4.
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void showOtherPage();
void showMainPage();
};
#endif // MAINWINDOW_H
mainiwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
qDebug() << "MainWindow - Debug Mode ON";
connect(ui->btnOther, SIGNAL(clicked()), SLOT(showOtherPage()));
}
void MainWindow::showOtherPage()
{
qDebug() << "Showing Other Page";
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::showMainPage()
{
qDebug() << "Showing Main Page";
ui->stackedWidget->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}

I had a very similar issue.
One of the UI actions was defined as the others and used.
I had a ui has no member named xyz compilation error for this one only, without possible explanation.
I could solve it by unchecking/checking the Shadow build option in the project compilation options!
Hope it saves someone the 30 minutes I just lost :)

Faced the very same issue today. Recreating the project does work, though I've found a solution that won't make one rewrite everything :) Run it in Debug. Once you've done it, problem's solved.

Creating a new project fixed it... I don't know why, I'm still looking for an explanation.
I started a new project, created the stackedWidget, and tested the code for the page-switching... it just simply works... and I still do not know WHY the other one fail to build...
Checked again and again names and everything.

I tried cleaning project and running qmake without any luck. I then drilled down into the project folder and deleted the ui_***.h file and everything started working. Might have had to do with the fact Visual Studio generated that file and I was working on another computer with Qt Creator for the same project.

Related

Qt MainWindow doesn't show MenuBar [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
In the code below I create Qt Widget's Application, base class QMainWindow, and without .ui form.
Cant understand why MenuBar doesn't show, tried different variants and no one works.
This image demonstrate what i got
.
System Ubuntu 16.04.
Using QMake version 3.0 and Qt version 5.5.1
Note: on other machines the same code works correctly.
Below mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include <QWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QMenu *file;
};
#endif // MAINWINDOW_H
Below mainwindow.cpp, commented lines show how I tried to fix it.
#include "mainwindow.h"
#include <QtGui>
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//QVBoxLayout *vbl = new QVBoxLayout;
QMenu *file = new QMenu("&File"); //menuBar()->addMenu("&File");//new QMenu("&File");
file->addAction("&Quit",qApp,SLOT(quit()),Qt::CTRL+Qt::Key_Q);
QMenuBar *mb = menuBar();
mb->addMenu(file);
mb->show();
setMenuBar(mb);
//vbl->setMenuBar(mb);
//setLayout(vbl);
resize(400,400);
}
MainWindow::~MainWindow()
{
}
After some investigations and reinstalling of all components I solved this simple problem. Need to change in 'System Settings -> Appearance -> Behavior' parameter for 'Show the menus for a window' from the "In the menu bar" to "In the window's title bar". Thanks to everyone who tried to help.

Qt C++ Creating toolbar

I am learning Qt and trying some examples in the book "Foundations of Qt Development".
In the book, there is a section teaching Single Document Interface with an example creating a simple app like a notepad.
However I am having problem with toolbar creating.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString("%1[*] - %2").arg("unnamed").arg("SDI"));
connect(ui->docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
createActions();
createMenu();
createToolbars();
statusBar()->showMessage("Done");
}
It is the constructor of the main window.
void MainWindow::createToolbars()
{
QToolBar* toolbar;
toolbar = addToolBar(tr("File"));
toolbar->addAction(anyaction);
}
This is how the book create the toolbar.
However, when I try to run the program, there are two toolbars created.
One is the toolbar created by the code and called "File"
Another is a blank toolbar created by the ui designer ie. *ui.toolbar.
In order to get rid of two toolbars, I tried using only the *ui.toolbar.
It's working. The code is shown below.
void MainWindow::createToolbars()
{
ui->toolBar->addAction(anyaction);
}
But I tried to create the toolbar by code only, ie. not adding a toolbar in the ui designer.
So I write this:
void MainWindow::createToolbars()
{
QToolBar* FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
However, there is a compile error.
The compiler use this function:
void QMainWindow::addToolBar(QT::ToolBarArea area, QToolBar * toolbar)
instead of what I want:
QToolBar * QMainWindow::addToolBar(const QString & title)
http://doc.qt.io/qt-5/qmainwindow.html#addToolBar-3
What is my mistake here?
When you removed QToolBar from MainWindow QtCreator automatically removed import of QToolBar class.
Just add this to the top of mainwindow.h:
#include <QToolBar>
And it is better to define QToolBar* FileBar in private section of MainWindow in mainwindow.h. Then you will be able to access it from any method of MainWindow class.
void MainWindow::createToolbars()
{
FileBar = this->addToolBar(tr("File"));
FileBar->addAction(anyaction);
}
When you see such message:
must point to class/struct/union/generic type
First of all try to include headers for necessary class.

Show QWidget or QWindow near QSystemTrayIcon in QT C++

I have managed to get the QSystemTrayIcon visible similar to this:
using the following line of code (with the signal slots working):
#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
#include <form.h>
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
QIcon icon("/Users/JohnnyAppleseed/IMAGE.png");
m_ptrTrayIcon = new QSystemTrayIcon(icon );
m_ptrTrayIcon->setToolTip( tr( "Bubble Message" ) );
// m_ptrTrayIcon->setContextMenu(m_trayIconMenu);
connect(m_ptrTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
Dialog::~Dialog()
{
delete ui;
}
However, when I try to implement code to show the QWidget/QWindow near the QSystemTrayIcon that I have created, it fails to show up near it. It also shows up and disappears quickly as well (even if I didn't want it near the QSystemTrayIcon) using this code:
void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
form fr;
fr.setWindowFlags(Qt::Popup);
fr.show();
}
For the sake of being clear, I would like to show my QWidget/QWindow just like VMWare Fusion's approach (or the clock that is found on Microsoft Windows Vista or later...)
Mac OS X / Linux
Microsoft Windows
Can some one please point out what am I doing wrong? Thanks!
To make things much simpler, download the project: http://zipshare.net/sv
UPDATE #1
Regarding the QWidget/QWindow flicking issue, vahancho advised me to move the form fr; from the void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason) function to the header of the working window. And it worked successfully all thanks to vahancho. The window now shows up, but its not near the QSystemTrayIcon yet :(
The problem is that you create you form object in the stack and it gets deleted as soon as the execution goes out of you iconActivated() slot. That is why it disappears as soon as you see it. To solve the problem you need to create your pop up in the heap.
UPDATE
In order to place you dialog near the tray icon you have to determine the tray icon position. To do that you can use QSystemTrayIcon::geometry() function. You code will look like (adjust the coordinates according to your needs):
QRect rect = m_ptrTrayIcon->geometry();
fr.move(rect.x(), rect.y());
fr.show();

Qt symbol(s) not found for architecture x86_64

First of all I am new to Qt, so if there may be something painstakingly obvious to some more advanced users, I also know this error crops up a lot and I've done a lot of reading on Qt forums and have still not managed to solve my problem.
I don't however understand why I am even getting the symbols not found for architecture x86_64 error as I had been working on my application all day without any problems.
I am building an image editor in the likes of Photoshop for an assignment in my Cross Platform SW Development course at Uni and wanted to separate the filter logic I was writing from the rest of my code, I therefore created a Filters class.
In my mainwindow.h I created a private reference to the class Filters *filter; which I instantiated in mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Instanciate a scene object that we can draw to and then set up the ui
scene = new QGraphicsScene(this);
filter = new Filters(this);
ui->setupUi(this);
QObject::connect(ui->cmdBlack_White, SIGNAL(triggered()), filter, SLOT(serialFilterBlackWhite(image)));
}
As you can see, I have created a connection to the serialFilterBlackWhite method, which resides in filters.cpp at the moment and doesn't do much due to the x86 error:
QPixmap Filters::serialFilterBlackWhite(QPixmap image)
{
image.fill( Qt::red );
return image;
}
The .h file for the class looks like this :
#ifndef FILTERS_H
#define FILTERS_H
#include <QObject>
#include <QPixmap>
class Filters : public QObject
{
Q_OBJECT
public:
explicit Filters(QWidget *parent = 0);
signals:
public slots:
QPixmap serialFilterBlackWhite(QPixmap);
};
#endif // FILTERS_H
I have tried not too bloat my question with too much code, but if you need more please let me know. I really have no idea whats going on with this.

QGLWidget crashes when added to layout

I am trying to adapt the opengl Es example "Hello GL" featured here - http://qt-project.org/doc/qt-4.8/opengl-hellogl-es.html. I am basically looking for a simple way to get a 3D graphics rendering window into a form made in Qt creator.
The first thing I tried:
Grid layout is a layout I created in Qt Creator.
#include <QProcess>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include "glwidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
GLWidget *glwidget = new GLWidget(); // This is mandatory. No problems here.
QTimer *timer = new QTimer(this); // Need this for the example to work.
timer->setInterval(10); // Also necessary.
ui->gridLayout->addWidget(glwidget);
Which compiles, but then promptly crashes with a segmentation fault.
ui->gridLayout->addWidget(new GLWidget);
Segfaults the same way.
The debugger points me toward line 104 of qgridlayout.h:
inline void addWidget(QWidget *w) { QLayout::addWidget(w); }
Not sure what to make of that. Perhaps the QGLWidget wants to do something before I call ui->setupUi(this)? Perhaps it can't add the widget to the layout for some reason?
Of course if I comment out the line where I am added the widget, the program works flawlessly.
Any ideas for what's going on here?
Edit: I have fixed this. It was problem with order of operations - I called updateui too quickly.
The setupUi function in the generated form class initializes all variables of the form class, so using the variables before the form is initialized is undefined behaviour, since the variables contain garbage.
So the solution is to call:
ui->setupUi(this)
Before any call that uses variables in the ui object.
I'm not sure, but I think the problem is with the following line:
ui->gridLayout->addWidget(GLWidget);
I think, you should write it as follows:
ui->gridLayout->addWidget(glwidget);
// declare glwidget as member of your class
GLWidget *glwidget;
//in constructor use
glwidget = new GLWidget();