This is the plot
And this is the code
.pro
QT += core gui widgets datavisualization
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
main.cpp
#include "mainwindow.cpp"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <QMainWindow>
#include <QtDataVisualization/Q3DBars>
using namespace QtDataVisualization;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
resize(800,600);
Q3DBars *graph = new Q3DBars;
setCentralWidget(QWidget::createWindowContainer(graph));
graph->scene()->activeCamera()->setCameraPosition(30,30);
graph->setBarSpacing(QSizeF(1,3));
QBar3DSeries *series = new QBar3DSeries;
graph->addSeries(series);
QStringList row_labels{"0", "", "20", "", "40"};
QStringList column_labels{"0", "", "20", "", "40"};
graph->rowAxis()->setRange(0, row_labels.count()-1);
graph->columnAxis()->setRange(0, column_labels.count()-1);
series->dataProxy()->setRowLabels(row_labels);
series->dataProxy()->setColumnLabels(column_labels);
// graph->activeTheme()->setGridEnabled(false);
}
MainWindow::~MainWindow(){}
Now to the question:
Can I have grid lines on vertical walls? I mean vertical lines in the picture, same way as in the floor.
Perhaps not possible with Q3DBars though with Q3DScatter maybe:
#include "mainwindow.h"
#include <Q3DScatter>
using namespace QtDataVisualization;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
auto chart = new Q3DScatter;
setCentralWidget(QWidget::createWindowContainer(chart));
auto series = new QScatter3DSeries;
chart->addSeries(series);
chart->axisX()->setRange(0,50);
chart->axisY()->setRange(0,50);
chart->axisZ()->setRange(0,50);
chart->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);
auto add_bar = [](int x, int y, int z){
auto bar = new QScatterDataArray;;
while(z-- > 0)
*bar << QVector3D(y, z, x);
return bar;
};
QList<QScatterDataArray*> row;
for(int i = 0; i < 5; i++)
row << add_bar(0, i*10, 20);
foreach (auto bar, row)
series->dataProxy()->addItems(*bar);
}
MainWindow::~MainWindow() {}
Related
For convenience, I created a function that helps me add new objects, but the MainWindow function doesn't accept them. So how can the MainWindow function receive the newly created object?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFrame>
#include <QVBoxLayout>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void CreateNewObj(int row, int column, int rowSpan, int columnSpan);
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);
for (int i = 0; i < 10; i++)
{
MainWindow::CreateNewObj(i,0,1,1);
}
// HERE
// this->ui->frame_1... IN HERE MainWindow function does not accept new object created by CreateNewObj function
// HERE
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::CreateNewObj(int row, int column, int rowSpan, int columnSpan)
{
QFrame *frame = new QFrame(this->ui->scrollAreaWidgetContents);
QVBoxLayout *verticalLayout;
verticalLayout = new QVBoxLayout(frame);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout_") + QString::number(row));
frame->setObjectName(QString::fromUtf8("frame_") + QString::number(row));
frame->setMinimumSize(QSize(100, 100));
frame->setMaximumSize(QSize(16777215, 50));
frame->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 170, 127);"));
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Raised);
this->ui->gridLayout->addWidget(frame, row, column, rowSpan, columnSpan);
}
mainwindow.ui:
It should be something like:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
QVBoxLayout *verticalLayout = new QVBoxLayout(ui->scrollAreaWidgetContents());
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
for (int i = 0; i < 10; i++)
{
QFrame *frame = new QFrame(ui->scrollAreaWidgetContents());
frame->setObjectName(QString::fromUtf8("frame_") + QString::number(i));
frame->setMinimumSize(QSize(100, 100));
frame->setMaximumSize(QSize(16777215, 50));
frame->setStyleSheet(QString::fromUtf8("background-color: rgb(255, 170, 127);"));
frame->setFrameShape(QFrame::StyledPanel);
frame->setFrameShadow(QFrame::Raised);
verticalLayout->addWidget(frame, i, 0, 1, 1);
}
}
Using QTCreator, I created the design of a GUI Application. I want to read the input entered by the user from lineEdit and when pushButton is clicked, it should print the factorial of that entered number on the same page. I've read some tutorials but don't understand how to code this using qtc++.
A minimal example is like that:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void hClicked();
void hTextEdit(const QString& data);
private:
QString m_linedata;
QPushButton button;
QLineEdit lineEdit;
QHBoxLayout layout;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
layout.addWidget(&lineEdit);
layout.addWidget(&button);
this->setLayout(&layout);
connect(&lineEdit, &QLineEdit::textChanged, this, &MainWindow::hTextEdit);
connect(&button, &QPushButton::clicked, this , &MainWindow::hClicked);
}
MainWindow::~MainWindow()
{
}
static unsigned factorial(unsigned n)
{
unsigned result = 1;
for (unsigned i=1; i <= n; i++) {
result *= i;
}
return result;
}
void MainWindow::hClicked()
{
if (m_linedata.size() > 0) {
bool res ;
int toint = m_linedata.toInt(&res);
if (res) {
unsigned fact_result = factorial(toint);
lineEdit.clear();
lineEdit.setText(QString::number(fact_result)); }
}
}
void MainWindow::hTextEdit(const QString &data)
{
m_linedata = data;
}
and main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Just do anything you like with the data passed to the auxillary buffer.
Step1: modify the right click menue
Step2: Qt program coding
// main.cpp
int main(int argc, char *argv[])
{
// QSharedMemory shared("Lakers");
// if(shared.attach()) {
// return 0;
// }
// shared.create(1);
QApplication a(argc, argv);
MainWindow w;
w.show();
w.showMainInputParas(argc, argv);
return a.exec();
}
// 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();
void showMainInputParas(int argc, char *argv[]);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#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::showMainInputParas(int argc, char *argv[])
{
for(int i=0; i<argc; i++) {
ui->textEdit->append(QString("%1: %2")
.arg(i)
.arg(QString::fromLocal8Bit(argv[i])));
}
}
Step3: select 2 files and open
Step4: show the 2 file paths in single program(wrong)
I want the 2 paths showed in one single program, and I add QSharememory class in main.cpp, but I still do not know how to pass the 2 file paths into main's argc & argv
Welcome I have a problem with scanning Wi-Fi to get all available connecting in Wi-Fi. I have writed so far this code:
#include <QCoreApplication>
#include <QNetworkConfigurationManager>
#include <QNetworkConfiguration>
#include <QDebug>
#include <QNetworkSession>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QNetworkConfigurationManager ncm;
QNetworkConfiguration cfg;
QNetworkConfiguration::StateFlags flags;
int count = QNetworkConfiguration::Active;
qDebug() << "Amount available connect in Wi-Fi :" << count;
qDebug() << ncm.allConfigurations(flags = 0);
return a.exec();
}
I have a problem with shows allConfigurations. I have read documentation
Qt Network Configuration Manager
but I do not know how to do that.
Scanning Wi-Fi using QNetworkAccessManager.
I use QNetworkConfigurationManager class to get all WiFi s availables and show all of them into QTreeWidget.
QNetworkConfigurationManager ncm;
netcfgList = ncm.allConfigurations();
.pro file:
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = WiFi
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
.cpp file:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
findTimer = new QTimer();
findTimer->setInterval(1000);
connect(findTimer,&QTimer::timeout,this,&MainWindow::findActiveWirelesses);
findTimer->start();
foundCount = 0;
ui->treeWidgetWiFis->setColumnWidth(0,50);
ui->treeWidgetWiFis->setColumnWidth(1,200);
findActiveWirelesses();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::findActiveWirelesses()
{
QNetworkConfigurationManager ncm;
netcfgList = ncm.allConfigurations();
WiFisList.clear();
for (auto &x : netcfgList)
{
if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
{
if(x.name() == "")
WiFisList << "Unknown(Other Network)";
else
WiFisList << x.name();
qDebug() << x.type();
}
}
for(int i=0; i<WiFisList.size(); i++)
{
bool exist = false;
QTreeWidgetItem * item = new QTreeWidgetItem();
for(int j=0; j<ui->treeWidgetWiFis->topLevelItemCount(); j++)
{
QTreeWidgetItem *index = ui->treeWidgetWiFis->topLevelItem(j);
QString str = index->text(1);
if(str == WiFisList[i])
{
exist = true;
break;
}
}
if(!exist)
{
item->setTextAlignment(0,Qt::AlignVCenter);
item->setTextAlignment(1,Qt::AlignHCenter);
item->setText(0,QString::number(++foundCount));
item->setText(1,WiFisList[i]);
ui->treeWidgetWiFis->addTopLevelItem(item);
}
}
}
.h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <QList>
#include <QInputDialog>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int foundCount;
QNetworkConfiguration netcfg;
QStringList WiFisList;
QList<QNetworkConfiguration> netcfgList;
public slots:
void findActiveWirelesses();
private:
Ui::MainWindow *ui;
QTimer *findTimer;
QStandardItemModel* listModel;
QNetworkSession *session;
};
#endif // MAINWINDOW_H
This question already has an answer here:
Qt/C++ Convert QString to Decimal
(1 answer)
Closed 8 years ago.
I am trying to make a program that takes 3 user inputs and a calculate button that puts them all into an equation and prints out the answer. The problem I am having right now is that the inputs seem to not be able to convert to numbers and I can't figure out why.
Error reads on line (int numN0 = QString::number(N0);):
error: no matching function for call to 'QString::number(QString&)'
Here's my code:
Header
#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 slots:
void on_NtButton_clicked();
void on_N0Button_clicked();
void on_kButton_clicked();
void on_tButton_clicked();
void on_quitButton_clicked();
void on_pushButton_5_clicked();
void on_equation_linkActivated(const QString &link);
private:
Ui::MainWindow *ui;
int N;
int N0;
int k;
int t;
};
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/*QPushButton *button = new QPushButton("Quit the program!");
QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
button->show();
*/
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_N0Button_clicked()
{
QString N0 = ui->lineEdit_2->text();
int numN0 = QString::number(N0);
if (numN0 < 1000){
QMessageBox::information(this,"Error","Can't be over 1000");
}
if (ui->lineEdit_2->text() > 0)
{
QMessageBox::information(this,"Error","Can't be under 0");
}
}
void MainWindow::on_kButton_clicked()
{
int k = ui->lineEdit_3->text();
if (QString::number(k) > 1)
{
QMessageBox::information(this,"Error","Can't be over 1");
}
if (ui->lineEdit_3->text() < 0)
{
QMessageBox::information(this,"Error","Can't be under 0");
}
}
void MainWindow::on_tButton_clicked()
{
QString t = ui->lineEdit_4->text();
}
void MainWindow::on_pushButton_5_clicked()
{
for (int x = 0; x < t; x++)
{
int ans = N*x == N0*10^(k*x);
ui->equation->setText(QString::number(ans));
}
}
You should use:
QString N0 = ui->lineEdit_2->text();
int numN0 = N0.toInt();
QString::number(N0) takes int and return QString, but you need conversion to int. Also you can use bool ok if you want to know is conversion was successful.
For example:
QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false
Information