[英]QT Slots and signals, showing 2nd form/window
I have a QT application and I'm trying to have a button in one of my windows open another window. 我有一个QT应用程序,我正试图在我的一个窗口中打开一个按钮打开另一个窗口。
The way I have done my window objects so far in the main is like this: 我到目前为止主要完成窗口对象的方式是这样的:
Website control;
control.show();
This displays my first window fine and if I declare my other window in a similar way that also displays at runtime, although this is not what I want 这显示我的第一个窗口很好,如果我以类似的方式声明我的其他窗口也在运行时显示,虽然这不是我想要的
Then in a separate header file: 然后在一个单独的头文件中:
class Website: public QWidget, public Ui::Website
{
public:
Website();
}
Then in the corresponding Cpp file I have: 然后在相应的Cpp文件中我有:
Website::Website()
{
setupUi(this);
}
Now all this works and have added a custom slot so that when I click a button it triggers a slot in my other cpp file. 现在所有这一切都有效,并添加了一个自定义插槽,以便当我单击一个按钮时,它会在我的其他cpp文件中触发一个插槽。 The issue is I'm not sure how to show my other window as I declare them in my main so can't access them to do .show()? 问题是我不知道如何显示我的其他窗口,因为我在我的主要声明它们所以无法访问它们来做.show()?
Any help would be appreciated, I'm fairly new to C++ and QT 任何帮助将不胜感激,我是C ++和QT的新手
It's not clear to me what you want to do. 我不清楚你想做什么。 But i might understand your struggle as I had one myself the first time approaching this framework. 但我可能理解你的斗争,因为我第一次接触这个框架时就有了自己的斗争。
So let's say you have a MainWindow class that controls the main Window view. 因此,假设您有一个MainWindow类来控制主窗口视图。 Than you want to create a second window controlled by Website class. 比想要创建由Website类控制的第二个窗口。
You then want to connect the two classes so that when you click a button on the Website window something happens in the MainWindow. 然后,您希望连接这两个类,以便当您单击“网站”窗口上的按钮时,MainWindow中会发生某些事情。
I made a simple example for you that is also on GitHub : 我为你做了一个简单的例子,也是在GitHub上 :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "website.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void changeText();
private slots:
void on_openButton_clicked();
private:
Ui::MainWindow *ui;
//You want to keep a pointer to a new Website window
Website* webWindow;
};
#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::changeText()
{
ui->text->setText("New Text");
delete webWindow;
}
void MainWindow::on_openButton_clicked()
{
webWindow = new Website();
QObject::connect(webWindow, SIGNAL(buttonPressed()), this, SLOT(changeText()));
webWindow->show();
}
#ifndef WEBSITE_H
#define WEBSITE_H
#include <QDialog>
namespace Ui {
class Website;
}
class Website : public QDialog
{
Q_OBJECT
public:
explicit Website(QWidget *parent = 0);
~Website();
signals:
void buttonPressed();
private slots:
void on_changeButton_clicked();
private:
Ui::Website *ui;
};
#endif // WEBSITE_H
#include "website.h"
#include "ui_website.h"
Website::Website(QWidget *parent) :
QDialog(parent),
ui(new Ui::Website)
{
ui->setupUi(this);
}
Website::~Website()
{
delete ui;
}
void Website::on_changeButton_clicked()
{
emit buttonPressed();
}
SOURCES += main.cpp\
mainwindow.cpp \
website.cpp
HEADERS += mainwindow.h \
website.h
FORMS += mainwindow.ui \
website.ui
Consider the Uis to be composed: 考虑组成Uis:
So the keypoints are the connections between signals and slots and the management of windows pointers or references. 因此,关键点是信号和插槽之间的连接以及窗口指针或引用的管理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.