qt networkManager get - qt

I want to download the url entered in the line edit widget.
I am not able to get it working , can some one please give me a short code snippet which can put the values of the file to a QString ?
void imdb::on_imdbGetButton_clicked()
{
Qstring link1 = ui->lineEdit2->text();
// QString link1 is the url to be downloaded.
}
I have added , the required header files..
Thanks..

I guess you're trying to download a file via http. Here's what you could do:
In you *.pro file add QT += network
Create an instance of QNetworkAccessManager class;
Supply your file URL to via QNetworkRequest object: manager->get(QNetworkRequest("file_url"));
Connect to the finished signal of the QNetworkAccessManager
In the finished signal handler read the content of the QNetworkReply and save it to the local file.
Below is a small example. Download will start in the button click of the MainForm class:
mainwindow.h:
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QFile>
#include <QFileInfo>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QNetworkAccessManager* _manager;
private slots:
void on_pushButton_clicked();
void downloadFinished(QNetworkReply *reply);
};
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton* button = new QPushButton("Download", this);
button->setGeometry(20, 20, 80, 30);
connect(button, SIGNAL(clicked()), SLOT(on_pushButton_clicked()));
_manager = new QNetworkAccessManager(this);
connect(_manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QUrl url("http://pics.mtii.com/ClassPictures2011/MIA/E110227-PMIA3-JEAN/thumbnails/P2270448%20copy.jpg");
_manager->get(QNetworkRequest(url));
}
void MainWindow::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error())
{
qDebug() << "Download of " << url.toEncoded().constData()
<< " failed: " << reply->errorString();
}
else
{
QString path = url.path();
QString fileName = QFileInfo(path).fileName();
if (fileName.isEmpty()) fileName = "download";
QFile file(fileName);
if (file.open(QIODevice::WriteOnly))
{
file.write(reply->readAll());
file.close();
}
qDebug() << "Download of " << url.toEncoded().constData()
<< " succeded saved to: " << fileName;
}
}
hope this helps, regards

Related

Problems not working QCanBus

Problems not create Can Bus Device, create project Qt 5.10 why not create ??
package serialbus be connected. CAN be connected across USB. IIts so hard. My life its harded. I'm a national minority, I'm not as clever as white masters, please do not grieve for me.
can bus Device not working
file.pro
QT += core gui serialbus
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CAN_simple_experiment
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSerialBus>
#include <QCanBus>
#include <QCanBusDevice>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
int i = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
// Create device.
QCanBusDevice *device =
QCanBus::instance()>createDevice("socketcan","vcan0");
if (device != nullptr){
qDebug() << "Created device, state is:" << device->state();
ui->textEdit->append("Created device, state is:"+ device->state());
} else {
qFatal("Unable to create CAN device.");
ui->textEdit->append("Unable to create CAN device.");
}
//Connect.
if(device->connectDevice()){
qDebug() << "Connected, state is:" << device->state();
ui->textEdit->append("Connected, state is:"+ device->state());
} else {
qDebug() << "Connect failed, error is:" << device->errorString();
ui->textEdit->append("Connect failed, error is:"+ device->errorString());
}
}
You need first to check SocketCAN interfaces that can be used check documentation.
QString errorString;
const QList<QCanBusDeviceInfo> devices = QCanBus::instance()->availableDevices(
QStringLiteral("socketcan"), &errorString);
if (!errorString.isEmpty())
qDebug() << errorString;
You can't create the device if the list is empty (watch the errorString);
// Create device.
if (devices.count())
QCanBusDevice *device =
QCanBus::instance()>createDevice("socketcan","vcan0");
There is no support of socketcan on Windows.
There are Linux specific calls inside socketcanbackend.cpp:
#include <linux/can/error.h>
#include <linux/can/raw.h>
#include <linux/sockios.h>
...
if (Q_UNLIKELY(ioctl(canSocket, SIOCGSTAMP, &timeStamp) < 0)) {
...
etc

Fill QTableWidget quickly

I'm creating a very simple disk space analyzer that shows ocupied size of every file in specific foder and its subfolders. I'm using QStack to store paths and QTableWidget for showing next information (File Name, File Path, File Size and Last Modified Date). First of all I use QDirIterator to fill QStack, then in different thread I get necessary information about file by QFileInfo and add row in QTableWidget with this information. Populating QStack is quick but filling QTableWidget is very slow. The question is how can I boost speed of filling QTableWidget.
dialog.h
#include <QDialog>
#include <QStack>
#include "mythread.h"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
MyThread *mThread;
QStack<QString> *fileStack;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
public slots:
void onFileAdded(QString file);
private:
Ui::Dialog *ui;
};
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "mythread.h"
#include <QFileDialog>
#include <QDirIterator>
#include <QMessageBox>
#include <QFileInfo>
#include <QDateTime>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "File name" << "File Path" << "File Size (KB)" << "Last Modified Date");
fileStack = new QStack<QString>();
ui->tableWidget->setSortingEnabled(true);
mThread = new MyThread(this);
connect(mThread, SIGNAL(fileAdded(QString)), this, SLOT(onFileAdded(QString)));
}
Dialog::~Dialog()
{
delete ui;
delete mThread;
delete fileStack;
}
void Dialog::onFileAdded(QString file) {
QFileInfo fileInfo;
fileInfo.setFile(file);
ui->tableWidget->insertRow(ui->tableWidget->rowCount());
ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
0,
new QTableWidgetItem(fileInfo.fileName()));
ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
1,
new QTableWidgetItem(fileInfo.filePath()));
int sss = fileInfo.size() / 1024;
QTableWidgetItem *item = new QTableWidgetItem;
item->setData(Qt::EditRole, sss);
ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
2,
item);
ui->tableWidget->setItem(ui->tableWidget->rowCount() - 1,
3,
new QTableWidgetItem(fileInfo.lastModified().toString()));
//ui->tableWidget->resizeColumnsToContents();
//ui->tableWidget->resizeRowsToContents();
}
void Dialog::on_pushButton_clicked()
{
QString dirname = QFileDialog::getExistingDirectory(this, tr("Select a Directory"), QDir::currentPath());
ui->lineEdit->setText(dirname);
}
void Dialog::on_pushButton_2_clicked()
{
QDirIterator it(ui->lineEdit->text(), QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
fileStack->push(it.next());
}
QMessageBox msgBox;
msgBox.setText("Completed");
msgBox.exec();
mThread->dir = fileStack;
mThread->start();
}

Drag and drop issue in Qt

I overload dragEnterEvent() and dropEvent() in my MainWindow class, and call setAcceptDrops() in the constructor. While the running, I drag a .txt file in to the texteditor, but it's not showing the content of that .txt. Instead, it's show the path of that .txt. Please somebody help where I did wrong. Thanks.
//Header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Blockquote
//Source
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDragEnterEvent>
#include <QUrl>
#include <QFile>
#include <QTextStream>
#include <QMimeData>
#include <QList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event){
if(event->mimeData()->hasUrls())
event->acceptProposedAction();
else event->ignore();
}
void MainWindow::dropEvent(QDropEvent *event){
const QMimeData *mimeData = event->mimeData();
if(mimeData->hasUrls()){
QList<QUrl> urlList = mimeData->urls();
QString fileName = urlList.at(0).toLocalFile();
if(! fileName.isEmpty()){
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly))return;
QTextStream in(&file);
ui->textEdit->setText(in.readAll());
}
}
}
You see this behavior because dropping is enabled on QTextEdit and the event is consumed there. (By default TextEdit drop copies filename into text area.)
In your constructor disable dropping on TextEdit by using
ui->textEdit->setAcceptDrops(false)
and then the event will be handled by the dropEvent method in MainWindow

Get duration of QMediaPlaylist object

I create a player for audiobooks - when you open a folder with mp3 file, whole list of them is added to playlist and List View. And i have a Label, which suppose to show duration of the whole book. But player->duration returns only a duration of current track, and if i go through the loop and do playlist->next() every step, player->duration returns 0. I know about Phonon and file metadata, but i need to do this without using it.
I am attaching a source code of a working project, you can use. When the player changes the file, the duration is changed and printed out. To loop within files, there is a need to wait till the decoder completes reading the media file. See the code below and the comments.
This is mainwindow.cpp
#include "mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
bool done =false;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
playlist = new QMediaPlaylist(player);
playlist->setPlaybackMode(QMediaPlaylist::Sequential);
player->setPlaylist(playlist);
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationchanged);
//connect(player,&QMediaPlayer::)
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
playlist->addMedia(QUrl::fromLocalFile("Ar_today.mp3"));
playlist->addMedia(QUrl::fromLocalFile("Ar_sunday.mp3"));
playlist->setCurrentIndex(0); //set the first file
while (done == false) //wait till the duration is read
{
QApplication::processEvents();
}
done = false; playlist->setCurrentIndex(1); //change to the second file
while (done == false) //wait till the duration is read
{
QApplication::processEvents();
} //this way you can loop through files
player->setVolume(80);
player->play();
qDebug() << player->errorString();
}
void MainWindow::on_pushButton_2_clicked()
{
player->stop();
}
void MainWindow::on_durationchanged(qint64 duration)
{
done = true;
qDebug() << "duration = " << player->duration();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QDebug>
extern bool done;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_durationchanged(qint64 duration);
private:
Ui::MainWindow *ui;
QMediaPlayer* player;
QMediaPlaylist* playlist;
};
#endif // MAINWINDOW_H
In the form, create 2 buttons, one called pushbutton to play and the other is pushButton_2 to stop

Windows Task Manager shows process memory keeps growing

I observed that through task mgr, the memory increases in steps of 4kB and 8kB, though not necessarily in this order.
Possible duplicate: Windows Task Manager shows process memory keeps growing even though there are no memory leaks
I am not sure whether this's occurring because I did not release the QTimer object, timer2. Please advise me how to stop this memory increase, and whether my guess of why it's occurring, is correct.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>
#include <QString>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define TIMER2_VALUE 3000
#define UPDATED_IMAGE_STORAGE_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\updatedRefImg.JPG"
#define UPDATED_IMAGE_BACKUP_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\backUp\\updatedRefImg[%1].JPG"
using namespace std;
using namespace cv;
typedef struct
{
QDateTime dateTimeMod1;
QDateTime dateTimeMod2;
}tTimeMods;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTimer *timer2;
tTimeMods findTimeModifiedStruct();
QDateTime findTimeModified();
void compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2);
QString appendWithImageName(tTimeMods timeTypeFunction);
void shiftToRepository(QString pathString);
void updatedImgToRepository(QString pathString);
public slots:
void timerSlot2();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
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"
tTimeMods timeTypeFunction, timeTypeMain;
QDateTime dateTimeMod2;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timeTypeMain = findTimeModifiedStruct();
timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(timerSlot2()));
timer2->start(TIMER2_VALUE);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerSlot2()
{
dateTimeMod2 = findTimeModified();
compareTimeMods(timeTypeMain, dateTimeMod2);
//delete timer2;
}
//tTimeMods findTimeModifiedStruct()
tTimeMods MainWindow::findTimeModifiedStruct()
{
QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
QFileInfo info(myFileName);
/*find last date modified*/
timeTypeFunction.dateTimeMod1 = info.lastModified();
timeTypeFunction.dateTimeMod2 = info.lastModified();
qDebug()<< "dateTimeMod1: " << timeTypeFunction.dateTimeMod1.toString() << endl << "dateTimeMod2: "<< timeTypeFunction.dateTimeMod2.toString();
return(timeTypeFunction);
}
QDateTime MainWindow::findTimeModified()
{
QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
QFileInfo info(myFileName);
QDateTime dateTimeMod2 = info.lastModified();
qDebug()<< "dateTimeMod2: "<< dateTimeMod2.toString();
return(dateTimeMod2);
}
void MainWindow::compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2)
{
if(dateTimeMod2 >= timeTypeFunction.dateTimeMod1)
{
timeTypeFunction.dateTimeMod1 = dateTimeMod2;
QString pathString = appendWithImageName(timeTypeFunction);
shiftToRepository(pathString);
}
}
QString MainWindow::appendWithImageName(tTimeMods timeTypeFunction)
{
/*appending just the timeMod with the path & image name*/
QString path = QString(UPDATED_IMAGE_BACKUP_PATH).arg(timeTypeFunction.dateTimeMod1.toString());
qDebug()<< "path: " << path;
return path;
}
void MainWindow::shiftToRepository(QString pathString)
{
updatedImgToRepository(pathString);
}
void MainWindow::updatedImgToRepository(QString pathString)
{
pathString.replace(":","-");
pathString.replace(1,1,":");
qDebug()<<"pathString now: "<<pathString;
/*convert QString into char* */
QByteArray pathByteArray = pathString.toLocal8Bit();
const char *path = pathByteArray.data();
IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);
/*save the image*/
cvSaveImage(path,InputImg);
cvReleaseImage(&InputImg);
}
I'm not familiar with OpenCV, but it seems that these two lines cause you memory leak:
IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);
In the first line you are creating an object, but in the second you are assigning another object to the pointer without releasing the previous one, so the previous one gets leaked.
Is creating an image even needed, while you are going to load it?

Resources