Qpushbutton not being displayed - c++

I have the following code to display a pushbutton, i am new to qt and can't figure out why its not working.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtWidgets>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QWidget* wdg = new QWidget(this);
QPushButton *button = new QPushButton(wdg);
button->setText(tr("something"));
setCentralWidget(wdg);//line 1
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
if i remove line1 then the button is being displayed but it is not clickable.

When ui->setupUi(this); is called, the user interface that is created in Qt designer stored in .ui file is initialized. It constructs the widgets and calls setCentralWidget internally. So when it is called the central widget is replaced by the one you set.
Either make the user interface by your own and ignore ui->setupUi(this); or first call ui->setupUi(this); in the constructor and do the rest of initializations by your own if you wish.

Related

How to add a custom widget to the main window in Qt Creator

I am new to Qt. I took an example from here http://doc.qt.io/qt-5/qtmultimediawidgets-player-example.html.
Now I want to integrate the player in the main window. I created a Qt Widgets application project, I thought, that I would just have to edit the main window code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Player* player;
MainWindow::setCentralWidget(player);
}
But it doesn't work and I get the following error:
Starting /home/***/Documents/build-player-Desktop-Debug/player...
The program has unexpectedly finished.
/home/***/Documents/build-player-Desktop-Debug/player crashed
How can I integrate a custom widget which is written in code, without ui in a main window? Thank you in advance.
In your own MainWindow class you can add a widget to the layout of that MainWindow:
MyMainWindow::MyMainWindow(QWidget *parent) :
...
{
this->ui->setupUi(this);
QLabel *myLabel = new QLabel();
this->layout()->addWidget(myLabel);
}
Well, player can't be placed on the window if it is not initialized.
Write something like that :
Player *player = new Player();
I usually add a QWidget (or whatever widget type I'm extending) to my .ui-file in the designer and then promote it to the actual derived type. See the Qt docs for more info on promoting widgets. This means that I can set the base widget's properties and design the window as usual but still get an instance of my special class when the UI is instantiated.
MainWindow:MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SomeStupidWidget *ssw = new SomeStupidWidget(this); /* important! don't forget about passing "this" as argument, otherwise this could cause a memory leak(Qt handles object's lifetime by means of it's "owner" mechanism)*/
layout()->addWidget(ssw);
}

Why aren't my QRadioButtons appearing in my QGroupBox?

Here is my code, it's super simple:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGroupBox* genderGroupBox = new QGroupBox("Gender", ui->centralWidget);
QVBoxLayout* genderGroupBoxLayout = new QVBoxLayout(genderGroupBox);
QRadioButton* maleRadioButton = new QRadioButton("Male", genderGroupBox);
QRadioButton* femaleRadioButton = new QRadioButton("Female", genderGroupBox);
genderGroupBoxLayout->addStretch();
}
but for some reason my program looks like this:
I've programmed in Qt quite a bit and I am totally baffled as to why the radio buttons aren't showing up inside the group box. I've tried explicitly adding them with
genderGroupBoxLayout->addWidget(maleRadioButton);
and manually adding the layout via
genderGroupBox->setLayout(genderGroupBoxLayout);
even though neither should be necessary because of how they were constructed. It looks like the radio buttons don't have any parent at all.
Anyone have any ideas?
I'm coding using Qt Creator 3.1.2 on a mac. Also the .ui file is empty of everything except centralWidget, which has a grid layout.
Please refer to the documentation :
When you use a layout, you do not need to pass a parent when
constructing the child widgets. The layout will automatically reparent
the widgets (using QWidget::setParent()) so that they are children of
the widget on which the layout is installed.
Your code should look like the following :
QGroupBox* genderGroupBox = new QGroupBox("Gender");
QVBoxLayout* genderGroupBoxLayout = new QVBoxLayout;
QRadioButton* maleRadioButton = new QRadioButton("Male");
QRadioButton* femaleRadioButton = new QRadioButton("Female");
genderGroupBoxLayout->addWidget(maleRadioButton);
genderGroupBoxLayout->addWidget(femaleRadioButton);
genderGroupBoxLayout->addStretch();
genderGroupBox->setLayout(genderGroupBoxLayout);
ui->centralWidget->layout()->addWidget(genderGroupBox);

Button doesn't perform action

I'm trying to perform action, when user clicks on button.
My code is:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtUiTools/QUiLoader>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
createActions();
}
void MainWindow::test()
{
//QMessageBox::information(this, "Welcome", "Select first image.");
//QFileDialog::getOpenFileName(this, QT_TR_NOOP("Open Image"), "D:\\", QT_TR_NOOP("Image Files (*.png *.jpg *.bmp)"));
resize(100,500);
}
void MainWindow::createActions()
{
QWidget *centralWidget = this->centralWidget();
QPushButton *buttonBack = centralWidget->findChild<QPushButton *>("pushButton");
QObject::connect(buttonBack,SIGNAL(clicked()), this, SLOT(test()));
QAction *open = this->findChild<QAction *>("actionOpen");
//QMessageBox::information(this, "Welcome", open->text());
connect(open, SIGNAL(triggered()), this, SLOT(test()));
}
Function void MainWindow::test() is defined as SLOT in header file and I'm sure, that QPushButton *buttonBack isn't null. What I'm doing wrong?
In my code I tried also to perform action through QAction, but in this case, function is performed, when I close window.
It looks like you're setting all your actions in the destructor.
Think about what this is doing:
UI starts up all happy with the setupUi() call from the constructor
Throughout the lifetime of the UI, there are no buttons assigned to any slots. This means the signal from your button will never get to the slot test().
Upon exit of the UI, the button is connected to the slot.
If you want this to happen, that's cool, but if you want the button to connect to the slot while the UI is running, move your createActions() function into the constructor.
Good luck!

QWebView,How to determine whether this link is a new window?

code
News::News(QWidget *parent) :
QDialog(parent),
ui(new Ui::News)
{
ui->setupUi(this);
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(ui->webView,SIGNAL(linkClicked(QUrl)),this,SLOT(openUrl(QUrl)));
}
void News::openUrl(QUrl url){
if(the new window)
QDesktopServices::openUrl(url);
}else{
ui->webView->load(url);
}
}
How do I judge instead of a new window link function openUrl?
How I'm going to write this code、?
thanks!!!
You need to inherit your own class from QWebView and reimplement the createWindow() method. http://doc.qt.digia.com/qt/qwebview.html#createWindow

QGraphicsView noobie question

Trying to add text to QGraphicsView:
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QGraphicsScene scene;
scene.addText("Hello, world!");
ui->graphicsView->setScene(&scene);
}
But when the project running, there is nothing on the QGraphicsView.
Your QGraphicsScene scene is a local variable and it is deleted immeadiately after the Widget's constructor has been executed.
Change the scene to a private member variable of the Widget class.