Error while promoting Widget container in Qt - c++

I've created my own class in QT 5.4 with header and source file, then I've implemented there a video player. The problem is, that it's working fine when I'm placing it in the center of the main window or when I'm showing it in a separate window, but when I'm trying to create a Widget container in the main window and promote widget to it, it gives me some errors:
'videowindow' does not name a type
'widget' was not declared in this scope
expected type-specifier before 'videowindow'
So, here is the code:
videowindow.h - header of my implemented class
#ifndef VIDEOWINDOW_H
#define VIDEOWINDOW_H
#include "mainwindow.h"
class videoWindow
{
private:
QMediaPlayer *player;
QMediaPlaylist *playlist;
public:
videoWindow();
void video_enable();
void widget();
};
#endif // VIDEOWINDOW_H
videowindow.cpp - source
#include "videowindow.h"
#include "mainwindow.h"
videoWindow::videoWindow()
{
}
void videoWindow::video_enable()
{
player = new QMediaPlayer;
playlist = new QMediaPlaylist;
playlist->addMedia(QUrl::fromLocalFile("D:\\фото\\video\\P1150277.MOV"));
QVideoWidget *videoWid = new QVideoWidget;
player->setVideoOutput(videoWid);
videoWid->show();
player->setPlaylist(playlist);
playlist->setCurrentIndex(1); // start from a first video in the playlist
player->play();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtMultimedia>
#include <QtMultimediaWidgets>
#include <QtDebug>
#include <string>
#include <QGraphicsVideoItem>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "videowindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include "videowindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
InstantPlayer.pro - .pro file of the project
#-------------------------------------------------
#
# Project created by QtCreator 2016-06-24T11:37:26
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = InstantPlayers
TEMPLATE = app
#
QT += multimedia
QT += multimediawidgets
#
SOURCES += main.cpp\
mainwindow.cpp \
videowindow.cpp \
playerbase.cpp
HEADERS += mainwindow.h \
videowindow.h \
playerbase.h
FORMS += mainwindow.ui \
playerbase.ui

Related

Qudpsocket readyread() don't work in try-catch block

I wanna get message by UDP protocol.
If I create object to work with QUdpsocket in try-catch block, signal readyread() don't work.
But if I created UDPworker's object out of try-catch block - all OK.
What I did uncorrect in exceptions and Qt combo?
Is it about implementation QUdpsocket?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//MyUDP m_UDP; // its work
try
{
MyUDP m_UDP; // its not work! WHY?!
}
catch(...)
{
qDebug()<< "Unknown exception cautch!!!" << endl;
}
return a.exec();
}
pr.pro
QT += core gui network
CONFIG += c++14 console
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, 6650);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QUdpSocket>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
//Base class QObject
class MyUDP : public QObject
{
Q_OBJECT
public:
explicit MyUDP(QObject *parent = 0);
void SayHello();
public slots:
void readData(){
QMessageBox::information(0, "Внимание","Это очень важный текст", 0,0,0);
}
private:
QUdpSocket *socket;
};
#endif // MAINWINDOW_H
You are creating MyUDP inside a try block, not catching any error an getting out of the try/catch scope, so MyUDP is begin destroyed:
try {
MyUDP m_UDP; // Created in the stack, valid only inside the try block
}
catch (...)
{
}
//m_UDP is already destroyed and undefined here
I should add that you are using QObjects, and signaling/slots mechanism not in intended/orthodox way, but that is beyond your question. You should read the documentation about the QObject and signal/slot mechanism, Qt has plenty of information both on-line and in the QtAssistant application.

Invalid use of incomplete type 'class Ui::dialog (QT error )

I would like to make a simple QT mainwindow with the button to open a second window or dialog. I followed literally the step from the QT link "Using a Designer UI File in Your Application" and following the single inheritance example.
But QT gives 4 errors , which you will see a snapshot of below.
Now, what I did is I created a mainwindow in Qt designer, then I added a second form to the project , which will be the second dialog window when a button clicked. Because I created the form manually "mydialog.ui", I added class "mydialog.h and mydialog.cpp" and put the header of "ui-mydialog" in the source file "mydialog.cpp".
I' not sure what am I missing ?
Below is the code :
- mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include<QWidget>
class mydialog ;
namespace Ui {
class mydialog;
}
class mydialog : public QWidget
{
Q_OBJECT
public:
explicit mydialog(QWidget *parent = 0);
virtual ~mydialog();
private :
Ui::mydialog *ui;
};
#endif // MYDIALOG_H
- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore/QtGlobal>
#include <QMainWindow>
QT_USE_NAMESPACE
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class mydialog;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_Start_clicked();
private:
Ui::MainWindow *ui;
mydialog *dialog1;
};
#endif // MAINWINDOW_H
- mydialog.cpp
#include"mydialog.h"
#include "ui_mydialog.h"
mydialog::mydialog(QWidget *parent) : QWidget(parent), ui(new Ui::mydialog)
{
ui->setupUi(this);
}
mydialog::~mydialog()
{
delete ui;
}
- mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"mydialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dialog1 = new mydialog ;
}
MainWindow::~MainWindow()
{
delete ui;
delete dialog1;
}
void MainWindow::on_Start_clicked()
{
}
- main.cpp
#include"mainwindow.h"
#include<QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
- The .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2015-12-17T00:10:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = TestTool
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mydialog.cpp
HEADERS += mainwindow.h \
mydialog.h
FORMS += mainwindow.ui \
mydialog.ui
RESOURCES += \
misc.qrc
- Qt compilation output error
Compilation error
The generated file Ui_mydialog.h is :
#ifndef UI_MYDIALOG_H
#define UI_MYDIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QHeaderView>
QT_BEGIN_NAMESPACE
class Ui_Dialog
{
public:
QDialogButtonBox *buttonBox;
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(400, 300);
buttonBox = new QDialogButtonBox(Dialog);
buttonBox->setObjectName(QStringLiteral("buttonBox"));
buttonBox->setGeometry(QRect(30, 240, 341, 32));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
retranslateUi(Dialog);
QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));
QMetaObject::connectSlotsByName(Dialog);
} // setupUi
void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0));
} // retranslateUi
};
namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MYDIALOG_H
This is because of the diferences between the names in your UI- und C++sourcecode files.
For example if in your UI-sourcecode file you have a name like "StatusBarPart"
but the name of your class in the C++ file is "StatusBar"
<class>StatusBarPart</class>
<widget class="QWidget" name="StatusBarPart">
StatusBar::StatusBar(QWidget *parent)
: PartBase(parent),
ui(new Ui::StatusBar)
then you you get these error message you see.
Solution:
You can edit the UI file in some external editor and make the names equal. Save changes. Compile your app. Be happy ;-)
You are mixing the name of the ui file with the name of the Ui class (objectName of the top level widget in QtDesigner).
For example, if QtDesigner looks like that:
You'll get a class names Ui::CalculatorForm, whatever the .ui file name is.
Replace Ui::mydialog by Ui::Dialog (or whatever the class name is in your generated ui_mydialog.h file)

Display code output to mainwindow in QT

I have some code that I wrote in geany that distributes a total of 28 numbers to 7 letters.
Here is the code in Geany:
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
int specialstats()
{
int stats[7], x;
for (x=0;x<7;x++) stats[x]=0;
for (x=0;x<28;x++) stats[rand()%7]++;
cout<<"S: "<<stats[0]<<"\n";
cout<<"P: "<<stats[1]<<"\n";
cout<<"E: "<<stats[2]<<"\n";
cout<<"C: "<<stats[3]<<"\n";
cout<<"I: "<<stats[4]<<"\n";
cout<<"A: "<<stats[5]<<"\n";
cout<<"L: "<<stats[6]<<"\n";
return 0;
}
int main()
{
srand(time(NULL));
specialstats();
return 0;
}
So basically I want to put it into QT and have the output of that code show up in a window. Here is what I have there:
again.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = again
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
Here is my 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;
};
#endif // MAINWINDOW_H
Here is my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <iostream>
#include <cstdlib>
#include <time.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
int stats[7];
int x;
for (x=0;x<7;x++) stats[x]=0;
for (x=0;x<28;x++) stats[rand()%7]++;
QMainWindow mainWindow;
QLabel *label2 = new QLabel (&mainWindow);
label2->setText("S: "<<stats[0]<<"\n");
std::cout<<"P: \033[1;32m"<<stats[1]<<"\033[0m\n";
std::cout<<"E: \033[1;32m"<<stats[2]<<"\033[0m\n";
std::cout<<"C: \033[1;32m"<<stats[3]<<"\033[0m\n";
std::cout<<"I: \033[1;32m"<<stats[4]<<"\033[0m\n";
std::cout<<"A: \033[1;32m"<<stats[5]<<"\033[0m\n";
std::cout<<"L: \033[1;32m"<<stats[6]<<"\033[0m\n";
mainWindow.show();
return a.exec();
}
And my mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
The errors that I get when I try to run this are, and are in my main.cpp:
/home/administrator/again/main.cpp:20: error: invalid operands of types 'const char [5]' and 'int' to binary 'operator<<'
label2->setText("S: \n"<<stats[0]<<"\n");
^
and,
[main.o] Error 1
What do I do to make the output of this text show up in the window?? In Geany, it outputs to a console and works flawlessly. I just want to try new things out and I am a beginner, so go easy on me.
You cannot concatenate c-style strings with the << operator.
To fix your compile error, change line 20 of main.cpp to e.g.:
label2->setText(QString("S: %1").arg(stats[0]));
QString is the default type to handle strings in Qt. You can find fine documentation about QString here: http://doc.qt.io/qt-5/qstring.html
Besides the compiler error, your code has some issues with how you draw into QMainWindow. Essentially, besides the QMainWindow, you need:
a widget
set the widget as 'centralWidget' of QMainWindow
define a layout for this widget
and add all the QLabels you want to show to this layout
These operations could be done in your MainWindow class. Or as you did, in the main function.
Here is a very crude alternative implementation of your main function. There is still a lot of room for improvement, but it should demonstrate the basic principle.
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <QApplication>
#include <QLabel>
#include <QVBoxLayout>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
int stats[7] = {1,2,3,4,5,6,7};
QWidget *centralWidget = new QWidget();
QVBoxLayout *layout = new QVBoxLayout(&w);
centralWidget->setLayout(layout);
w.setCentralWidget(centralWidget);
QLabel *label[7];
for (int i=0;i<7;i++) {
label[i] = new QLabel (QString("S: %1").arg(stats[i]));
layout->addWidget(label[i]);
}
return a.exec();
}
If you use QtDesigner, you don't have to worry about how to define the layouts programmatically, but you can use a nice and intuitive tool to help you with the process of designing the layout and appearance of the windows.
QtDesigner generates the *.ui files.

QtCreator: Loading External Library

So I've been developing a small ACARS (plane tracking system) for the VATSIM network and apart of this entails using the FSUIPC library to interact with Flight Sim X. I have the program working beautifully in VS2010 but when I attempt to use any functions from FSUIPC I get the error:
"mainwindow.obj:-1: error: LNK2019: unresolved external symbol FSUIPC_Open referenced in **function "public: void __cdecl MainWindow::connectFSUIPC(void)" (?connectFSUIPC#MainWindow##QEAAXXZ)"**
From some research I've learnt that this is a library load error and for the life of me I can't figure out how to get it working. Below is the code for all files I am using.
AcarsTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2013-12-16T23:56:06
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = AcarsTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/"
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <Windows.h>
#include <FSUIPC_User.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void connectFSUIPC();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include <Windows.h>
#include "mainwindow.h"
#include <QApplication>
#include "FSUIPC_User.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectFSUIPC()
{
DWORD dwResult;
if(FSUIPC_Open(SIM_ANY, &dwResult))
{
}
}
try change code like this
your code:
LIBS += -LD:/Windows/QT/Tools/QtCreator/bin/AcarsTest/ -lFSUIPC_User
replace by
win32:LIBS += "D:/Windows/QT/Tools/QtCreator/bin/AcarsTest/FSUIPC_User.lib"

(QWebView) QWidget: Must construct a QApplication before a QPaintDevice

Just trying to make a simple web viewer app so I can view a webpage in a widget and I am getting the error:
QWidget: Must construct a QApplication before a QPaintDevice
I feel like this is a problem with the webkit? I am statically linking this project.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QWebPage>
#include <QtWebKit>
#include "Windows.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWebView *view = new QWebView(parent);
view->load(QUrl("http://google.com/"));
view->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
.pro file
QT += core gui webkit network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = webkittest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
header file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebView>
#include <QUrl>
#include <QtPlugin>
#include <QGridLayout>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QMessageBox>
#include <QDialog>
#include <QtWebKit>
class QAction;
class QCheckBox;
class QComboBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QMenu;
class QPushButton;
class QSpinBox;
class QTextEdit;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
//void closeEvent(QCloseEvent *event);
//void keyPressEvent(QKeyEvent *);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
private slots:
//void onSslErrors(QNetworkReply* reply, const QList<QSslError> &errors);
private:
Ui::MainWindow *ui;
QWebView* m_pWebView;
#endif // MAINWINDOW_H
Any ideas what could be causing that?
Fixed this by installing the latest version of Qt framework and dynamically linking webkit as they have disable static linking.