qt specific implementation - qt

I have this snippet of the code:
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QWidget>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
QPushButton *quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),
lcd, SLOT(display(int)))
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
when I reach the max of the interval, I want to quit, how can I implement this with signals and slots, thanks in advance

Add one more slot-function to your MyWidget class, for example on_maximum_exit(int) like that:
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
public slots:
void on_maximum_exit(int );
};
Define new function:
void MyWidget::on_maximum_exit(int value)
{
if (value == slider->maximum())
close();
}
In MyWidget consctuctor, after connect(slider, SIGNAL(valueChanged(int)),... add:
/* ... */
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(on_maximum_exit(int)));
/* ... */
And of course use Qt docs!

Like I said in the other post this should work for you:
main.cpp
#include <QApplication>
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QObject>
include <QPushButton>
#include <QSlider>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
public slots:
void getSliderValueAndQuit();
private:
QPushButton *quit;
QSlider *slider;
};
#endif // MYWIDGET_H
myWidget.cpp
#include "mywidget.h"
#include <QWidget>
#include <QObject>
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
close();
}

Related

How to use QTimer to view a image sequence?

In the program, I am trying to create a image player using QT. When I click a button in the UI, the program will create a image slideshow with a 2s pause. I tried to use the QTimer to such things, but failed to do so. Hence, I want to ask how to achieve my purpose by using QTimer.
Let me describe the flow of my program. When the user click a button in the main window, the sub-window showpic will be opened and then start showing each image for a pause of 2s in its qgraphsview. The images filepath are stored in the "QStringlist filenames".
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileSystemModel>
#include "showpic.h"
#include <QBasicTimer>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void display(const QString & , ShowPic* );
private slots:
void tick();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QFileSystemModel *model;
QString filesPath;
ShowPic *showpic;
QStringList filenames;
QStringList::const_iterator m_imageIt;
QTimer m_timer;
};
#endif // MAINWINDOW_H
showpic.h
#ifndef SHOWPIC_H
#define SHOWPIC_H
#include <QWidget>
namespace Ui {
class ShowPic;
}
class ShowPic : public QWidget
{
Q_OBJECT
public:
explicit ShowPic(QWidget *parent = 0);
~ShowPic();
private:
Ui::ShowPic *ui;
public:
void addPixmap(const QPixmap &pixmap);
};
#endif // SHOWPIC_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"
#include<QFileDialog>
#include<QFileSystemModel>
#include<QStringList>
#include <QTreeView>
#include <QGraphicsScene>
#include <QTime>
#include <QThread>
#include <QDebug>
#include <iostream>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
filenames.append("C:\\test\\image.jpg");
filenames.append("C:\\test\\apple.jpg");
filenames.append("C:\\test\\orange.jpg");
filenames.append("C:\\test\\lemon.jpg");
filenames.append("C:\\test\\grape.jpg");
m_imageIt = filenames.begin();
m_timer.setInterval(5000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::display(const QString & filename, ShowPic* showpic) {
showpic->addPixmap(filename);
}
void MainWindow::tick(){
showpic = new ShowPic();
showpic->show();
display(*m_imageIt, showpic);
m_imageIt ++;
}
/*
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() == m_timer.timerId()) tick();
}*/
void MainWindow::on_pushButton_clicked()
{
/*showpic = new ShowPic();
QPixmap pixmap("C:\\test\\image.jpg");
showpic->addPixmap(pixmap);
showpic->show();*/
m_timer.start();
}
showpic.cpp
#include "showpic.h"
#include "ui_showpic.h"
#include <QThread>
ShowPic::ShowPic(QWidget *parent) :
QWidget(parent),
ui(new Ui::ShowPic)
{
ui->setupUi(this);
ui->graphicsView->setScene(new QGraphicsScene);
}
ShowPic::~ShowPic()
{
delete ui;
}
void ShowPic::addPixmap(const QPixmap &pixmap){
ui->graphicsView->scene()->addPixmap(pixmap);
}
The compiling message error:
The error has nothing to do with the timer, it is because you forgot to declare display(), tick(), and timerEvent() as part of the MainWindow:: class, so they cannot access MainWindow members.
The timer should be even easier to use than your code. First I recommend you use a QTimer instead of QBasicTimer. Then you can simply connect to its timeout() signal.
mainwindow.h
#include <QTimer>
QTimer m_timer;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
...
m_imageIt = filenames.begin();
m_timer.setInterval(5000);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));
}
void MainWindow::on_pushButton_clicked()
{
m_timer.start();
}
You do not need timerEvent() function at all.

QScrollArea and QVBoxLayout issues with dynamically added widgets

Im running into a weird issue with dynamically added widgets to a QVBoxLayout contained inside a QScrollArea. If I add the widgets it works as expected, however after all widgets are removed, there are still some artifacts on the screen. See screenshot bellow:
See the code bellow:
ui_mainwindow.h
/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 5.2.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralWidget;
QVBoxLayout *verticalLayout_2;
QScrollArea *scrollArea;
QWidget *scrollAreaWidgetContents;
QHBoxLayout *horizontalLayout_2;
QVBoxLayout *verticalLayout;
QPushButton *pushButton;
QPushButton *pushButton_2;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QStringLiteral("MainWindow"));
MainWindow->resize(600, 396);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
verticalLayout_2 = new QVBoxLayout(centralWidget);
verticalLayout_2->setSpacing(6);
verticalLayout_2->setContentsMargins(11, 11, 11, 11);
verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
scrollArea = new QScrollArea(centralWidget);
scrollArea->setObjectName(QStringLiteral("scrollArea"));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 574, 246));
horizontalLayout_2 = new QHBoxLayout(scrollAreaWidgetContents);
horizontalLayout_2->setSpacing(6);
horizontalLayout_2->setContentsMargins(11, 11, 11, 11);
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
verticalLayout = new QVBoxLayout();
verticalLayout->setSpacing(6);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
horizontalLayout_2->addLayout(verticalLayout);
scrollArea->setWidget(scrollAreaWidgetContents);
verticalLayout_2->addWidget(scrollArea);
pushButton = new QPushButton(centralWidget);
pushButton->setObjectName(QStringLiteral("pushButton"));
verticalLayout_2->addWidget(pushButton);
pushButton_2 = new QPushButton(centralWidget);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
verticalLayout_2->addWidget(pushButton_2);
MainWindow->setCentralWidget(centralWidget);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QStringLiteral("menuBar"));
menuBar->setGeometry(QRect(0, 0, 600, 22));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QStringLiteral("statusBar"));
MainWindow->setStatusBar(statusBar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
pushButton->setText(QApplication::translate("MainWindow", "add one", 0));
pushButton_2->setText(QApplication::translate("MainWindow", "remove one", 0));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
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 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"
#include <QLabel>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->verticalLayout->addWidget(new QLabel("This is a label", this));
qDebug() << "Labels count: " << ui->verticalLayout->count();
}
void MainWindow::on_pushButton_2_clicked()
{
delete ui->verticalLayout->takeAt(0);
qDebug() << "Labels count: " << ui->verticalLayout->count();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
It seems like I also have to remove the QLayoutItem AND the Widget, just removing the widget or the QLayoutItem (which I did try) doesn't work, both have to be removed, like so:
QLayoutItem *child = ui->verticalLayout->takeAt(0);
if (child)
{
delete child->widget();
delete child;
}

Why I can't connect QAction SIGNAL with QFileDialog SLOT?

I'm trying to write application's menu for my text editor, but I don't understand why signal triggered does not working. It should open a QFileDialog::getOpenFileName, but it does not happen. Why?
main.cpp
#include "mainwindow.h"
#include "centralwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *mainWindow = new MainWindow;
CentralWidget *centralWidget = new CentralWidget;
mainWindow->setCentralWidget(centralWidget);
mainWindow->show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QStatusBar>
#include <QAction>
#include <QFileDialog>
#include <QString>
class MainWindow : public QMainWindow
{
public:
MainWindow();
public slots:
void slotFileDialog();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
QMenu *fileMenu = new QMenu("File");
QAction *openAction = new QAction(tr("&Open..."), this);
connect(openAction, SIGNAL(triggered()),
this, SLOT(slotFileDialog()));
fileMenu->addAction(openAction);
QMenuBar *mainMenu = new QMenuBar;
mainMenu->addMenu(fileMenu);
mainMenu->show();
QStatusBar *mainStatusBar = new QStatusBar;
setMenuBar(mainMenu);
setStatusBar(mainStatusBar);
}
void MainWindow::slotFileDialog()
{
QString fileName = QFileDialog::getOpenFileName(
this, tr("Open File"),"/home",tr("Text (*.txt)"));
}
You haven't included the Q_OBJECT macro in the class definition, so the moc will not generate the associated files for it.

how to use ui file for making a simple widget?

i have a simple window with a quit button in qt.The working code is shown below
#include <QApplication>
#include <QDialog>
#include <QPushButton>
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(200, 120);
QPushButton *btquit = new QPushButton(tr("Quit"), this);
btquit->setGeometry(62, 40, 75, 30);
btquit->setFont(QFont("Times", 18, QFont::Bold));
connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
Now i want to code this program using qt designer.I created a widget named "mywindow" and a button inside that main widget named "btquit" in the ui file using qt designer.
How to rewrite the above code with the ui file.The name of ui file is mywindow.ui
#include <QApplication>
#include <QDialog>
#include <QPushButton>
#include "ui_mywindow1.h"
class MyWidget : public QWidget,private Ui::mywindow
{
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
I prefer to have the ui as private member in the widget's class. I assume that in the designer you have named the widget as mywindow (the objectName from the properties).
// MyWindow.h
#include <QWidget>
// Forward declaration of your ui widget
namespace Ui {
class mywindow;
}
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
~MyWidget();
private:
// private pointer to your ui
Ui::mywidget *ui;
};
And then in your .cpp you have to do the following:
#include "mywindow.h"
//1. Include the auto generated h file from uic
#include "ui_mywindow.h"
#include <QPushButton>
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent),
//2. initialize the ui
ui(new Ui::mywindow)
{
//3. Setup the ui
ui->setupUi(this);
// your code follows
setFixedSize(200, 120);
QPushButton *btquit = new QPushButton(tr("Quit"), this);
btquit->setGeometry(62, 40, 75, 30);
btquit->setFont(QFont("Times", 18, QFont::Bold));
connect(btquit, SIGNAL(clicked()), qApp, SLOT(quit()));
}
MyWidget::~Mywidget()
{
delete ui;
}

how to get number of the current position of the cursor (qt)

I need small help, I'm using class Qslider from qt, how can I get the number of the current position of the cursor (with which function I can do that) thanks in advance
edited
I want to implement one thing: when I reach the max of the interval I want to quit, how can I do this with slot and signal?
I am assuming you want the value of the slider?
int QSlider::value ()
I looked at the code in your other post and this is what I came up with after cleaning it up:
main.cpp
#include <QApplication>
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QObject>
include <QPushButton>
#include <QSlider>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
~MyWidget(){};
public slots:
void getSliderValueAndQuit();
private:
QPushButton *quit;
QSlider *slider;
};
#endif // MYWIDGET_H
myWidget.cpp
#include "mywidget.h"
#include <QWidget>
#include <QObject>
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
quit = new QPushButton(tr("Quit"));
quit->setFont(QFont("Times", 18, QFont::Bold));
QLCDNumber *lcd = new QLCDNumber(3);
lcd->setSegmentStyle(QLCDNumber::Flat);
slider = new QSlider(Qt::Horizontal);
slider->setRange(0, 999);
slider->setValue(0);
connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(slider, SIGNAL(valueChanged(int)),lcd, SLOT(display(int)));
connect(slider,SIGNAL(sliderReleased()), SLOT(getSliderValueAndQuit()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(lcd);
layout->addWidget(slider);
setLayout(layout);
}
void MyWidget::getSliderValueAndQuit(){
if(slider->value() == slider->maximum())
close();
}

Resources