Error occurs when use a QScrollArea - qt

#include <QtCore>
#include <QtGui>
#include <QLabel>
#include <QScrollArea>
#include <QScrollBar>
#include <QFileDialog>
#include"QDebug"
#include<math.h>
#include<QApplication>
QScrollArea* scrollarea = new QScrollArea;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setBackgroundRole(QPalette::Base);
ui->label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->label->setScaledContents(true);
scrollarea->setBackgroundRole(QPalette::Dark);
scrollarea->setWidget(ui->label);
setCentralWidget(scrollarea);
QImage img("D:\\picture.jpg");
ui->label->setPixmap(QPixmap::fromImage(img));
}
MainWindow::~MainWindow()
{
delete ui;
}
i am newbie in Qtcreator.
i try to add scroll bar to label. but when i run, it occurs an error:
QWidget: Must construct a QApplication before a QPaintDevice
how should i do to fix this

QScrollArea* scrollarea = new QScrollArea;
You are constructing a global QScrollArea before QApplication is created. Make it a member variable of MainWindow.

Related

Something wrong with QGraphicsScene y-acxis

I have a simple example with QGraphicsScene wrong behavior
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene * scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
//background
ui->graphicsView->setBackgroundBrush(Qt::black);
QLineF y_line(ui->graphicsView->sceneRect().center().x(), ui->graphicsView->sceneRect().center().y() - 100,
ui->graphicsView->sceneRect().center().x(), ui->graphicsView->sceneRect().center().y() - 550);
QLineF x_line(ui->graphicsView->sceneRect().center().x() - 100, ui->graphicsView->sceneRect().center().y(),
ui->graphicsView->sceneRect().center().x() - 550, ui->graphicsView->sceneRect().center().y());
scene->addLine(y_line, QPen(Qt::yellow));
//add line X
scene->addLine(x_line, QPen(Qt::green));
ui->graphicsView->fitInView(ui->graphicsView->sceneRect());
}
MainWindow::~MainWindow()
{
delete ui;
}
I uave this:
And this is ok, i think
BUT
if i comment out add line X
i have this :
How you can see the line direction!! changed .
How somebody can explain this???
Wrong behavior i think? some bugs in Qt?

Qt Main Window not repainting MDI area

I have an application that have a main window with an MDI area that uses the whole window. When the application is opened it is not maximized. If i open the child window and then close it, the ghost of the window still shows. If i open a another child window and then move it across the MDI area you get copies of the window one on top of each other only in the area where the ghost was.
If i open the main window, then maximize it, open the child and maximize it, this problem disappears. Then a can get the main window to the original size, open and close child windows, move them around and the background is redrawn correctly.
Is there anything that can be done to solve this behavior?
Calling the mdi window:
void PriceAnalysisTool::on_actionImport_triggered()
{
importprocess = new ImportProcess(working, printer, ui->mdiArea);
importprocess->show();
}
And the initial part of the sub window:
ImportProcess::ImportProcess(DataBase *d, QPrinter *p, QWidget *parent) : QMdiSubWindow(parent), ui(new Ui::ImportProcess)
{
ui->setupUi(this);
Test program showing the same behavior:
main window:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include "subwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_Window_triggered();
private:
Ui::MainWindow *ui;
SubWindow *subwindow;
};
#endif // MAINWINDOW_H
main window.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::on_actionOpen_Window_triggered()
{
subwindow = new SubWindow(ui->mdiArea);
subwindow->show();
}
subwindo.h
#ifndef SUBWINDOW_H
#define SUBWINDOW_H
#include <QWidget>
#include <QMdiSubWindow>
namespace Ui {
class SubWindow;
}
class SubWindow : public QMdiSubWindow
{
Q_OBJECT
public:
explicit SubWindow(QWidget *parent = 0);
~SubWindow();
private:
Ui::SubWindow *ui;
};
#endif // SUBWINDOW_H
subwindw.cpp
#include "subwindow.h"
#include "ui_subwindow.h"
SubWindow::SubWindow(QWidget *parent) : QMdiSubWindow(parent), ui(new Ui::SubWindow)
{
ui->setupUi(this);
}
SubWindow::~SubWindow()
{
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();
}
the ui for the main window has only has a MDI are and the a menu with a single item that calls the subwindow. The subwindow is a widget that has nothing on it.
{
QSqlQuery query;
query.exec("drop table C"+QString::number(markTime.toSecsSinceEpoch()));
query.exec("drop table C"+QString::number(markTime.toSecsSinceEpoch())+"T");
query.exec("drop table C"+QString::number(markTime.toSecsSinceEpoch())+"S");
query.exec("drop table C"+QString::number(markTime.toSecsSinceEpoch())+"F");
delete ui;
}

Read multiple texts Qt

i am doing Qt project about displaying many texts. in detail, after 1st text display, it will close then display next file. My problem here was that just the last file displayed. all link resource paths are correct. Please help me fix me. Thanks in advance
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList L;
L << ":/sample.txt" << ":/idp.txt";
foreach (QString str, L){
QFile file(str);
if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(0,"error file path", file.errorString());
QString name = file.fileName();
QStringList parts = name.split("/");
QString lastBit = parts.at(parts.size()-1);
statusBar()->showMessage(lastBit);
QTextStream out(&file);
QString txt = out.readAll();
QStackedWidget *temp = new QStackedWidget();
QTextBrowser *textbrs = new QTextBrowser();
textbrs->setText(txt);
temp->addWidget(textbrs);
setCentralWidget(temp);
file.close();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Something like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QString>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
L << ":/sample.txt" << ":/idp.txt"; //Class member of Type QStringList
textbrs = new QTextBrowser(); //Class member of Type QTextBrowser*
ui->centralWidget->layout()->addWidget(textbrs);
timer = new QTimer(); //Class member of Type QTimer*
timer->setInterval(5000);
connect(timer,SIGNAL(timeout()),this,SLOT(slotFileAction()));
timer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::slotFileAction()
{
static int count = 0;
if(count >= L.size())
timer->stop();
QString str = L.at(count);
count++;
QFile file(str);
if (!file.open(QIODevice::ReadOnly))
QMessageBox::information(0,"error file path", file.errorString());
QString name = file.fileName();
QStringList parts = name.split("/");
QString lastBit = parts.at(parts.size()-1);
statusBar()->showMessage(lastBit);
QTextStream out(&file);
QString txt = out.readAll();
textbrs->setText(txt);
file.close();
}
Each time you will replace the old QStackedWidget by
setCentralWidget(temp);
The example to use QStackedWidget in Qt help document is following:
QWidget *firstPageWidget = new QWidget;
QWidget *secondPageWidget = new QWidget;
QWidget *thirdPageWidget = new QWidget;
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(firstPageWidget);
stackedWidget->addWidget(secondPageWidget);
stackedWidget->addWidget(thirdPageWidget);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
So, you should add multiple QTextBrowsers in QStackedWidget by addWidget() and use setCentralWiget() just once.
Hope helpful.

PopUP Window in Qt

I am creating an application in Qt and I have a problem. I have a main window and I want that when I push a button, a popup window appears. I dont know how can I do it. I tried to call the show() method when I push the button but dont work. I think that I must use the exec() method from QApplication but I dont know how can call it if I created it in the main class.
My classes:
#include "mainwindow.h"
#include "dialog.h"
#include <QApplication>
#include "popup1.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QApplication>
int posiciones[10];
std::string port="";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
for (int i=1; i<10; i++){
if(i==7){
posiciones[i]=90;
}
posiciones[i]=0;
}
//Mandar el vector para mover
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
PopUp:
#include "popup1.h"
#include "ui_popup1.h"
Popup1::Popup1(QWidget *parent) :
QDialog(parent),
ui(new Ui::Popup1)
{
ui->setupUi(this);
}
Popup1::~Popup1()
{
delete ui;
}
Anyone knows how can I show the popup window? Thanks for your time.
Connect your button signal clicked() with exec() slot of your popup window:
connect(pushButton, SIGNAL(clicked()), popupWindow, SLOT(exec()));
Where pushButton - pointer to your button, and popupWindow - pointer to your popup window. You can write this code in QMainWindow constructor.
Include your pop-up class in your MainWindow.h:
include "popup.h"
Define a pointer to your pop-up class in the MainWindow.h:
popup1 *mpPopUp1;
Create an object in the MainWindow.cpp file:
mpPopUp1 = new popup1;
Define a slot in MainWindow.h and call it, for example, showPopUp():
void showPopUp();
Create the slot showPopUp() in your MainWindow.cpp and write the following statement inside it:
mpPopUp1 ->show();
Connect your pushButton to the slot showPopUp():
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(showPopUp()));
Run you app, click on pushButton and voila

How to run thread from push button?

main.cpp:
#include "mainwindow.h"
#include "myobject.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QThread cThread;
MyObject cObject;
cObject.doSetup(cThread);
cObject.moveToThread(&cThread);
// cThread.start();
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::on_pushButton_clicked()
{
// cThread.start();
}
I'm trying to start the thread on on_pushButton_clicked(). In order to do this, I would have to pass cThread to mainwindow through an argument, right? I just want someone to confirm to make sure I'm doing it right.
This seems like one way to do that. The other one would be to connect signal clicked() of button to thread's slot start(Priority) in your application which will be nicer and shorter.

Resources