Display code output to mainwindow in QT - c++

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.

Related

Access widgets in QMainWindow from another class

Although it is a frequently asked question, and I've tried many ways including those from SO, like Trying to access widgets of MainWindow from another class, However I still cannot work out a solution, below is my code which reported error "Unknown type name 'CustomClass'" in mainwindow.h:
Thanks in advance for any help!
customclass.h
#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
class MainWindow;
class CustomClass
{
public:
CustomClass(MainWindow *parent);
MainWindow * mainWindow;
void testFunc();
};
#endif // CUSTOMCLASS_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "customclass.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
Ui::MainWindow *ui;
CustomClass *customClass = new CustomClass(this);
};
#endif // MAINWINDOW_H
customclass.cpp
#include "customclass.h"
CustomClass::CustomClass(MainWindow *parent)
{
this->mainWindow = parent;
}
void CustomClass::testFunc()
{
mainWindow->ui->label->setText("Hello World!");
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
customClass->testFunc();
}
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();
}
please read about Circular Dependencies in C++ .
you have problems because you create a loop, you included #include "customclass.h" in MainWindow class and also #include "mainwindow.h" in CustomClass .
it's better that you don't use MainWindow in other classes and add CustomClass objects in MainWindow. The idea is that MainWindow is your root window class and we create one object from it in main.cpp you can do what you ask but logically it's not good.
as we can see in QMainWindow Class document.
The QMainWindow class provides a main application window
A main window provides a framework for building an application's user
interface.
This is your base, you should add the feature you want inside this class not add it in other widgets.
It's the main class for other widgets. This is a clean way to code and if you look at big projects in GitHub you will see this.
move #include "mainwindow.h" and #include "ui_mainwindow.h" inside customclass.h into customclass.cpp before #include "customclass.h" and it works! Thank you!#drescherjm
The fixed code is shown below:
customclass.h
#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H
class MainWindow;
class CustomClass
{
public:
CustomClass(MainWindow *parent);
~CustomClass();
MainWindow *mainWindow;
void testFunc();
};
#endif // CUSTOMCLASS_H
customclass.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"//Move these two lines from the header to here
#include "customclass.h"
CustomClass::CustomClass(MainWindow *parent)
{
this->mainWindow = parent;
}
CustomClass::~CustomClass()
{
}
void CustomClass::testFunc()
{
mainWindow->ui->label->setText("Hello World!");
}

Qt find word function with QWebEngine

I create a simple web browser with Qt 5.7 and the webEngine.
I would like to create an search function to find word in webView but I don't really know how make interaction between web content and Qt ( I'm new on Qt, I do this for fun and to improve my level ).
this is the minimal code too display WebView :
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
return a.exec();
}
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWebEngineWidgets>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void setWebEngine();
private:
Ui::MainWindow *ui;
QWebEngineView *view;
};
#endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWebEngine();
setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setWebEngine()
{
view = new QWebEngineView();
view->load(QUrl("http://www.google.com"));
}
I would like to know something else, is it possible to list elements in the web content ( form, h1, h2, ... )
Have a nice day, and sorry for my English ;)
From the Qt5 documentation
void QWebEngineView::findText(const QString &subString, QWebEnginePage::FindFlags options = QWebEnginePage::FindFlags())
Finds the specified string, subString, in the page, using the given
options.
To clear the selection, just pass an empty string.
See also selectedText() and selectionChanged().
Should be quite simple.

C++/Qt - error: C2027: use of undefined type 'QBitmap'

I'm trying to add an image to a QLabel on a Qt creator program. However when compiling the program I get an error: C:\Users\*****\Documents\GameAPP\main.cpp:12: error: C2027: use of undefined type 'QBitmap'
Using Qt version 5.7 and 5.6 on Windows 10.
This is my code:
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QLabel *l = new QLabel();
QPixmap p("C:\Users\*****\Pictures\Start_Orb.png");
l->setPixmap(p);
l->setMask(p.mask()); //error at this line
l->setFixedSize(20, 20);
l->move(20, 20);
l->show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QLabel>
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"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
You only need to add #include <QBitmap> and it will work.

Error while promoting Widget container in Qt

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

(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.