How can I call the "pushButton" in main.cpp? - qt

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
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;
}
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QObject::connect(pushButton, SIGNAL(clicked()),
&a, SLOT(quit()));
return a.exec();
}
All the codes are given above. In a general Qt GUI program, I put a pushButton on the UI Form and tried to use it in main.cpp. But below error was given:
main.cpp:10: Error:'pushButton' was not declared in this scope
Could you please give me a solution? How can I call it in main.cpp?
Thanks!
Complement1:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->pushButton, SIGNAL(clicked()),
QCoreApplication::instance(), SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}
If I do it this way, then the program can run but cannot close the whole application. I guess that is because the QCoreApplication::instance() returns null due to in the constructor phase the QApplication doesn't exist, right?
Complement2:
mainwindow.cpp
void MainWindow::on_pushButton_clicked()
{
close();
}
One solution is to add new slot of the pushButton in mainwindow.cpp, like above.
But still I hope to know how to do it my way (main part of this post)?
Complement3:
Alberto's code works fine by using QWidget as below way.
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));

Your first approach can't work because, as compiler says, you didn't defined a QPushButton in main.cpp scope. So compiler can't find it.
Right approach is your last one. You develop a .ui file in which you add a QPushButton and then in c++ code that handles your .ui file(in your example a QMainWindow subclass) you connect it with a slot. But your connect call is wrong. This is my code:
//widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
//widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
Widget::~Widget()
{
delete ui;
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
}
//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>93</width>
<height>41</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
//main.cpp:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
with: connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
which we can simplify in this way: connect(1,2,3,4);
when 2 is emitted because user does something on 1, do 4 on 3.
translated: when user click on pushButton(defined into ui file), do close on this(QMainWindow subclass). And if you take a look at your main.cpp code, MainWindow is the main widget you show to the user. So you're closing the whole app.

Related

Disable scrolling to left/right/up/down in Qwebengineview

I want my qwebengineview can't scroll to the left/right/up/down.
My situation is like this:
It was asked in Qt Fourm first, however I can't find a solution for this.
QT Fourm link: link
Please see the minimum code example code below. It's not the same website but can also show this problem.
I'm using Mac Os 10.15 and Qt 5.13.1.
Update
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWebEngineView>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QWebEngineView* m_pWebView;
};
#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);
this->setFixedSize(920,580);
m_pWebView = new QWebEngineView(this);
// Set WebView Size
m_pWebView->setGeometry(0,0,920,580);
// Disable Right Click
m_pWebView->setContextMenuPolicy(Qt::NoContextMenu);
m_pWebView->load(QUrl("https://clash.razord.top/#/proxies"));
}
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();
}

Give error when compile program in qt 5.8

I get this error when run program in qt 5.8 in windows 10 64 bit. how to solve this issue.
My .pro file
00:43:14: Running steps for project untitled1...
00:43:16: Starting: "G:\IDE\Qt5.8.0\5.8\mingw53_32\bin\qmake.exe" "G:\Qt Projects\untitled1\untitled1.pro" -spec win32-g++ "CONFIG+=qml_debug"
MAKEFILE_GENERATOR variable not set as a result of parsing : untitled1.pro. Possibly qmake was not able to find files included using "include(..)" - enable qmake debugging to investigate more.
00:43:32: The process "G:\IDE\Qt5.8.0\5.8\mingw53_32\bin\qmake.exe" exited with code 3.
Error while building/deploying project untitled1 (kit: Desktop Qt 5.8.0 MinGW 32bit)
When executing step "qmake"
00:43:32: Elapsed time: 00:18.
Header File
#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
main.cpp file
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui file
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
please help me to fix this issue. Thanks

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

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