Missing some functions in my Qt class [closed] - c++

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm starting using Qt for a GUI, but I have some problems with headers/libraries because I'm missing some functions.
Two of them are:
<obj_name>.setModal(true);
<obj_name>.exec();
They should work fine as in the video I'm following (at 6:30).
Because I did exactly what they did, so my clue is his version isn't the same as mine.
I want to know which header should I include.
Here's my code:
void MainWindow::on_actionNew_Window_triggered()
{
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
}
Even with:
#include <QDialog>
Still doesn't work. It says:
C:\QtSDK\teste-build-desktop-Qt_4_8_1_for_Desktop_-MinGW_Qt_SDK__Debug..\teste\mainwindow.cpp:22: error: 'class MyDialog' has no member named 'setModal'.
mydialog.h code:
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QMainWindow>
#include <QDialog>
namespace Ui {
class MyDialog;
}
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_H
It is included in mainwindow.cpp and mydialog.cpp (the header is just the class).

MyDialog is no QDialog. You created it as a "main window" which is no dialog.
To hot-fix this (without recreating the dialog using QtCreator), just rewrite the inheritance in mydialog.h
from:
class MyDialog : public QMainWindow
to:
class MyDialog : public QDialog
In your mydialog.cpp you find the implementation of the constructor of MyDialog which calls the superclass constructor. Since we just changed the superclass, we also have to change this call from:
MyDialog::MyDialog(QWidget *parent) :
QMainWindow(parent)
...
to
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent)
...
You also have to fix your .ui file to morph the whole widget from a main window to a dialog. I'll add how to do so in a few minutes (have to find it out) You don't need to touch the .ui file.

You're trying to call setModal() from MyDialog class, but it inherits from QMainWindow, which has no setModal method. You must inherit from QDialog instead.

Related

QT Signal and Slot porblem:Is `No such slot` related to Protected Signal?

I was trying to connect signal QListWidget->itemChanged to a custom Slot called checkItemChanged. I asked for right syntax in this question.
I'm using QT4 so I used old-string syntax as below to connect signal and slot:
connect(listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this , SLOT(checkItemChanged(QListWidgetItem*)));
This connect syntax is declared in a file called mainWindow.cpp and its header file mainWindow.h is as below:
class mainWindow : public QWidget
{
Q_OBJECT
public:
mainWindow ( QWidget *parent=0, const char *name=0 );
public slots:
void checkItemChanged(QListWidgetItem*);
I also defined my slot implementation in mainWindow.cpp as below:
void mainWindow::checkItemChanged(QListWidgetItem* item)
{
if (item->checkState()==true){
qDebug()<<"Item: "<<item->text()<<"Checked";
}else{
qDebug()<<"Item: "<<item->text()<<"UnChecked";
}
}
So it used macro Q_OBJECT as answered here as common reason for no such slot error.
I run my project in KDevelop IDE, so it will automatically build .moc file and run qmake as asked in this answer
However still I face error as below:
Object::connect: No such slot mainWindow::checkItemChanged(QListWidgetItem*) in /home/part/Desktop/project/P/src/gui/mainWindow.cpp:320
I know this is strange but I want to know may be it is related to the fact that QListWidget->itemChanged is protected? Or there are some other thing that I have missed?

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.

How to add QwtPlot as a base class in Qt promote?

I am using Qt creator 4.2.1. I have installed Qwt lib successfully and been able to drag and drop QwtPlot widget to my form. I have an custom class Plot:public QwtPlot. I wish I can add QwtPlot to my form and then perform a widget promote to Plot.
However, when I try to do so, the promote dialog box does not show QwtPlot as a base class that I can choose. If I select QWidget as the base class and then specify "Plot" and "plot.h" as my promote class and header file, I got build errors complaining redefine of "class Plot". I do have the plot.h in my project already and it seems uic creates another "class Plot" for me. Hence, the compiler complains my plot.h has an redefined class.
How to resolve this problem? Thanks!
Place a widget from toolbox and use promote and make sure your class structure is like following sample.
#ifndef PLOT_H
#define PLOT_H
#include <QObject>
#include <qwt_plot.h>
class Plot : public QwtPlot
{
Q_OBJECT
public:
explicit Plot(QWidget *parent = nullptr);
signals:
public slots:
};
#endif // PLOT_H

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.

Qt Creator no member named stackedWidget

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.