How do you create a QPushButton in the MainWindow? - qt

QPushButton* m_button = new QPushButton();
Whenever i try to create a QPushButton like this, it appears to be created in a new separate window. However, I want the Button to be created within the Mainwindow but I don't know how to do this (without using the Qt Designer).

You need to set the MainWindow as a parent
try this:
QPushButton* m_button = new QPushButton(this)

Related

close button on qDialog only closing on second click

I'm trying to generate a dialog that contains an ad-on tool that is separate from my main program, it its triggered from an action within the menus.
I've got the following code:
void MainWindow::on_actionCalibration_Tool_triggered()
{
QGridLayout *grid = new QGridLayout;
NewDialog.setLayout(grid);
NewDialog.setMinimumHeight(500);
NewDialog.setMinimumWidth(800);
QLabel *label = new QLabel;
QFont sansFont("MS Shell Dlg 2",22, QFont::Bold);
label->setText("Test");
label->setFont(sansFont);
QPushButton *okbutton = new QPushButton;
QPushButton *closebutton = new QPushButton;
okbutton->setText("Ok");
closebutton->setText("Close");
QTimer *timer = new QTimer;
connect(okbutton,SIGNAL(clicked()),this,SLOT(on_ScanpB_clicked()));
connect(closebutton,SIGNAL(clicked()),this,SLOT(CloseDialog()));
grid->addWidget(label);
grid->addWidget(okbutton);
grid->addWidget(closebutton);
NewDialog.exec();
NewDialog.show();
}
void MainWindow::CloseDialog()
{
NewDialog.close();
}
With NewDialog being defined in main window.h as a QDialog.
My issue is when I click the close button, the dialog will close for a split second then reopen, after I click the close button for a second time it closes for good.
Is there any better implementation or way around this?
Thanks
You should not call QDialog::show and QDialog::exec. Instead, pick one to call.
Use exec if you want to block user interaction with the dialog's parent while the dialog is open. The user will not be play with anything else in the application until they dismiss the dialog. This is called a modal.
Use show if you want to allow the user to work with the dialog and the rest of the application at the same time.
Usually you'd choose exec. It is easier to work with. In your case, you displayed the dialog twice by calling both functions.

How to undock tab with osgViewer from QTabWidget?

I want to undock a QWidget from a QTabWiget (is set as centralWidget). The tab contains some Open Scene Graph content (OpenGL Window). When removing the Tab from the list and putting it into a new Dialog Window (=> undocking from tab) the scene data seems to be corrupt. It works with "standard widgets" but the osg seems to forget the scene.
Surprisingly, undocking works when using a QDockWidget (scene is visible after undocking the window).
Anyone knows how to undock a tab without corrupting the osgViewer?
Code called for to undock from tab and show in new dialog window:
QWidget* gv = // points to an osgViewer in a qt widget
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
gv->setMinimumSize(100,100);
gv->setGeometry(100,100,300,300);
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
dlg->show(); // should show the undocked dialog
There is nothing to see in the new dialog. Did I missed something?
How to "copy" the osg view properly into a new widget/dialog? Should I use a composite viewer for this kind of task? It seems there is not even the empty osg view visible (no blue canvas)...
It could be that something's going screwy when you add the osgViewer to another widget before removing it from the QTabWidget. Changing the order might help.
QWidget* gv = // points to an osgViewer in a qt widget
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
dlg->show(); // should show the undocked dialog

Qt Designer Shortcut to another tab

I was wondering if it were possible to create my own shortcut key to a QTabWidget. So if I put an ampersand infront of the letter, that means that ALT+'letter' will display that tab; however, I want it so that CTRL+'letter' will display that tab (not ALT).
Is there an easy way to do this in Qt Designer? If not, is there a simple way to do it in code? QTabWidget doesn't seem to have any direct methods for setting shortcuts.
I don't know of a way to do this via the Designer, not familiar with that. You could do it with QShortcut fairly easily in code though.
Here's a dummy widget to illustrate that. Press Ctrl+a / Ctrl+b to switch between tabs.
#include <QtGui>
class W: public QWidget
{
Q_OBJECT
public:
W(QWidget *parent=0): QWidget(parent)
{
// Create a dummy tab widget thing
QTabWidget *tw = new QTabWidget(this);
QLabel *l1 = new QLabel("hello");
QLabel *l2 = new QLabel("world");
tw->addTab(l1, "one");
tw->addTab(l2, "two");
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(tw);
setLayout(l);
// Setup a signal mapper to avoid creating custom slots for each tab
QSignalMapper *m = new QSignalMapper(this);
// Setup the shortcut for the first tab
QShortcut *s1 = new QShortcut(QKeySequence("Ctrl+a"), this);
connect(s1, SIGNAL(activated()), m, SLOT(map()));
m->setMapping(s1, 0);
// Setup the shortcut for the second tab
QShortcut *s2 = new QShortcut(QKeySequence("Ctrl+b"), this);
connect(s2, SIGNAL(activated()), m, SLOT(map()));
m->setMapping(s2, 1);
// Wire the signal mapper to the tab widget index change slot
connect(m, SIGNAL(mapped(int)), tw, SLOT(setCurrentIndex(int)));
}
};
This isn't meant as an example of widget layout best practices... just to illustrate one way to wire a shortcut sequence to a tab change.

Creating a Symbian's like application menu at S60

I wanted to make an app which is when started it shows a menu. It is just like when you open a Messaging app in S60. Here is a screenshot.
How do I make it? I have tried to make the centralWidget of the QMainWindow as QMenu, and adding QAction to the QMenu. But when I'm running it, the app don't show anything. And I have tried to make the QMenu using QMenuBar. And it is show okay. But I can't use the up/down key to select menu in the device. And when I press the options key (Qt::PositiveSoftKey), the menubar shows up too. And I didn't even add that to menuBar() which is owned by QMainWindow.
Here is my first code:
QAction* act1= new QAction(tr("act1"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QAction* act2= new QAction(tr("act2"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QMenu* menu = new QMenu(this);
menu->addAction(act1);
menu->addAction(act2);
setCentralWidget(menu);
And it shows nothing at the apps.
And here is my second try:
Qt Code: Switch view
QAction* act1= new QAction(tr("act1"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QAction* act2= new QAction(tr("act2"),this);
QObject::connect(tes,SIGNAL(triggered()),this,SLOT(close()));
QMenuBar* menubar = new QMenuBar(this);
QMenu* menu = menubar->addMenu(tr("menu"));
menu->addAction(act1);
menu->addAction(act2);
setCentralWidget(menu);
It shows the menu. But when I deploy to the device, I can't use the keypad to select the menu. And at the simulator, if I click other place than the QAction item, the menu lost.
I am using another approach by using QPushButton with Vertical Layout. Here is the code:
QWidget* centralWidget = new QWidget(this);
QScrollArea* scrollArea = new QScrollArea(this);
scrollArea->setWidget(centralWidget);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
QVBoxLayout* centralLayout = new QVBoxLayout(centralWidget);
QPushButton* button1 = new QPushButton(tr("button 1"));
QPushButton* button2 = new QPushButton(tr("button 2"));
centralLayout->addWidget(button1);
centralLayout->addWidget(button2);
centralLayout->setContentsMargins(0,0,0,0);
button1->setFocus();
Here it's look:
Okay, and that's look good enough. But if that's have 8 button. If it is only have 2 button, it looks like this:
looks kind of weird, huh? Any way to prevent this?
For me, the UI that you're trying to replicate is a list of options, so, why don't try to create the UI based on a list widget?
Asumming that you're going to use a list, you have to options from which you have to choose based on your needs and your app requirements:
Qt Widgets: Available in all the Qt supported Plataforms. Created for desktop and works great on it, but in feel strange on mobile devices. Take a look to the QListWidget and QListView.
Qt Quick: It isn't available for S40 phones or S60 3rd edition, not available for non-touch phones, basically. Created for "touch enabled UI's", it doesn't offer a stable set of widgets (buttons, comboboxes) yet, but it offers a set of primitives (like rectangles or images) that gives you a lot of freedom to create your UI's and it looks pretty good. I think that this is what Lucian used to create the UI from his answer.
This examples could be of particular interest: http://doc.qt.io/archives/qt-4.7/all-examples.html
Run the QtDemo to see all the examples live!
Hope it helps!
EDIT: Adding example of QListWidget
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i=0; i<10; i++) {
QListWidgetItem *item = new QListWidgetItem(QIcon("Qt.png"), QString("Item %1").arg(i));
ui->listWidget->insertItem(i, item);
}
ui->listWidget->setIconSize(QSize(64, 64));
connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(onItemClicked(QListWidgetItem*)));
}
void MainWindow::onItemClicked(QListWidgetItem *item)
{
QMessageBox::information(this, "", QString("%1 pressed").arg(item->text()));
}
You have all the freedom in the world to create your layout the way you like. Want those buttons closer together? Want them centered in the view?
The attached image took me 30 seconds to create and looks already decent (according to my pour designer skills).

Problems with QDialog in Qt

I'm using Qt for Symbian. I have some problems with a QDialog that I open from a QMenu. The QDialog shows up fine and in the QDialog I have a QDialogButtonBox with a button to Close the QDialog. BUT if I close the QDialog and then open it from the QMenu again, it will show up but the button from the QDialogButtonBox will not show up. Instead the buttons from the QMainWindow will show but they are grayed out.
How can I get the QDialog buttons to show every time? Maybe I have some problems with setting focus on the QDialog? I really can't see what I'm doing wrong here.
It's not much code that I use, you can try it yourself. This is my code:
In QMainWindow I use the following to create the menu:
QAction *menuButton = new QAction("Menu", this);
menuButton->setSoftKeyRole(QAction::PositiveSoftKey);
QMenu *menu = new QMenu(this);
menuButton->setMenu(menu);
QAction *popup = new QAction("Show popup",this);
connect(popup, SIGNAL(triggered()), this, SLOT(showPopup()));
menu->addAction(popup);
addAction(menuButton);
This shows the QDialog:
void MyMainWindow::showPopup(){
TestDialog *test = new TestDialog(this);
test->setAttribute(Qt::WA_DeleteOnClose);
test->show();
}
This is the TestDialog:
TestDialog::TestDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
this->setFixedWidth(rect.width());
}
If you want your dialog to be modal, use exec(). Otherwise, you should use show() and raise() to make sur it's on top.

Resources