I want to show or hide items in QStackedWidget. When I press Enter button it should show a stacked element and when I press say a left button it should hide.
I use QStackedWidget and QListWidget. My code:
mymainwindow.h:
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
class mymainwindow : public QMainWindow
{
Q_OBJECT
public:
mymainwindow();
protected:
void keyPressEvent(QKeyEvent *event);
private:
QStackedWidget *stack;
QListWidget *list;
QVBoxLayout *vertical;
QWidget *widget;
};
#endif
mymainwindow.cpp:
#include "mymainwindow.h"
mymainwindow::mymainwindow() : QMainWindow()
{
stack = new QStackedWidget();
list = new QListWidget();
stack->addWidget(new QLineEdit("Hello U have clicked the first menu"));
stack->addWidget(new QLineEdit("Second ListWidget Item"));
stack->addWidget(new QLineEdit("Last Widget Item"));
widget = new QWidget();
QLabel *label = new QLabel("Main Window");
list->addItem("New Item 1");
list->addItem("New Item 2");
list->addItem("New Item 3");
list->setFixedSize(200,100);
QVBoxLayout *vertical = new QVBoxLayout();
vertical->addWidget(label);
vertical->addWidget(list);
vertical->addWidget(stack);
widget->setLayout(vertical);
setCentralWidget(widget);
}
void mymainwindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Down:
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
break;
case Qt::Key_Up:
connect(list, SIGNAL(currentRowChanged(int)), stack, SLOT(setCurrentIndex(int)));
break;
case Qt::Key_Left:
break;
}
}
You will need to handle the Key_Left and Key_Enter cases in your key press event handler. It seems that you would simply like to show and hide the stackwidget based on the press of those two buttons. This is a simple QWidget operation, and the problem is not much related to QStackedWidget.
You would need to change the key press event code as follows:
void mymainwindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Down:
connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
break;
case Qt::Key_Up:
connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));
break;
case Qt::Key_Left:
stack->show(); // <---- Added
break;
case Qt::Key_Enter: // <---- Added
stack->hide(); // <---- Added
break; // <---- Added
}
}
Related
I have a QComboBox with several choices on a QToolBar. Every choice of the QComboBox will open a specific dialog window.
The problem I have is that after I choose the preferred index on the combobox no dialogs opens up. For simplicity of the example I am linking the same dialog to all choices:
dredgewindow.h
This is the header file
namespace Ui {
class DredgeWindow;
}
class DredgeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit DredgeWindow(QWidget *parent = nullptr);
~DredgeWindow();
private:
Ui::DredgeWindow *ui;
DredgeDB *mDredgeDB;
};
dredgewindow.cpp
DredgeWindow::DredgeWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::DredgeWindow)
{
ui->setupUi(this);
QComboBox* myComboBox = new QComboBox;
ui->toolBarControls->addWidget(myComboBox);
myComboBox->addItem("Please Select");
myComboBox->addItem("Bucket");
myComboBox->addItem("Scow");
myComboBox->addItem("Hopper Dredger");
switch(myComboBox->currentIndex()){
case 0:
// do nothing
break;
case 1:
// Go to Bucket
mDredgeDB = new DredgeDB();
mDredgeDB->show();
break;
case 2:
// Go to Scow...
mDredgeDB = new DredgeDB();
mDredgeDB->show();
break;
case 3:
// Go to Hopper Dredger
mDredgeDB = new DredgeDB();
mDredgeDB->show();
default:
break;
}
}
DredgeWindow::~DredgeWindow()
{
delete ui;
}
So far I have been trying to trigger the opening of the dialogs using the combobox but as soon as I release the mouse (and therefore I switch - case) I expect the dialog to open but nothing happens. This source was useful even though it was not in c++. But still I used it to understand the general approach.
This approach triggers the combobox and set it active but other than that there is no specific indication.
Thanks in advance for pointing to the right direction for solving this problem.
You have to connect QComboBox::activated() signal to some slot.
Signals & Slots in Qt5
New Signal Slot Syntax
qOverload<>
This signal is sent when the user chooses an item in the combobox. The
item's index is passed. Note that this signal is sent even when the
choice is not changed. If you need to know when the choice actually
changes, use signal currentIndexChanged().
Note: Signal activated is overloaded in this class. To connect to this
signal by using the function pointer syntax, Qt provides a convenient
helper for obtaining the function pointer - qOverload<>.
class DredgeWindow : public QMainWindow
{
Q_OBJECT
...
private slots:
void on_combo_index_activated(int index);
...
};
DredgeWindow::DredgeWindow(QWidget *parent)
: QMainWindow(parent)
{
...
connect(myComboBox, QOverload<int>::of(&QComboBox::activated),
[=](int index) { on_combo_index_activated(index); });
...
}
void DredgeWindow::on_combo_index_activated(int index)
{
switch (index)
{
case 0:
// do nothing
break;
case 1:
// Go to Bucket
mDredgeDB = new DredgeDB();
mDredgeDB->show();
break;
case 2:
// Go to Scow...
mDredgeDB = new DredgeDB();
mDredgeDB->show();
break;
case 3:
// Go to Hopper Dredger
mDredgeDB = new DredgeDB();
mDredgeDB->show();
default:
break;
}
}
I'm trying to show custom widgets in a scroll area, but it only shows the last one.
Scroll area contents has to change dinamicly with combo box index, and they change and show the last element too.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fileMenu = ui->menuBar->addMenu(tr("&Archivo"));
openFileAction = new QAction(tr("Abir archivo"), this);
connect(openFileAction,
SIGNAL(triggered()),
this,
SLOT(openFile()));
fileMenu->addAction(openFileAction);
scroll = ui-> scrollArea;
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidgetResizable(true);
ui->stackedWidget->setCurrentIndex(0);
}
vector<product> MainWindow::filter(QString cad)
{
vector <product> tempList;
QMessageBox message;
for(size_t i(0);i<products.size();i++){
if(products.at(i).getId().contains(cad)){
product *p = new product;
p->setId(products.at(i).getId());
p->setName(products.at(i).getName());
p->setPrice(products.at(i).getPrice());
tempList.push_back(*p);
}
}
return tempList;
}
void MainWindow::loadproducts(int category){
QMessageBox message;
vector <product> tempList;
switch(category){
case Alimentos:{
tempList=filter("AB");
break;
}
case Libros:{
tempList=filter("L");
break;
}
case Electronicos:{
tempList=filter("E");
break;
}
case Hogar:{
tempList=filter("HC");
break;
}
case Deporte:{
tempList=filter("D");
break;
}
case Todos:{
tempList=products;
break;
}
default:{
break;
}
}
//THIS FOR IS SUPPOSED TO ADD WIDGETS TO SCROLL AREA
for(size_t i=0;i<tempList.size();i++){
ProductWidget *p = new ProductWidget(widget, tempList.at(i).getId(), tempList.at(i).getName(), tempList.at(i).getPrice());
scroll->setWidget(p);
}
tempList.clear();
}
Scroll area has to show 10 widgets, but it only shows the last one.
You can only set a widget in the QScrollArea using the setWidget() method, if another one is added it will replace the previous one. If you want to show several widgets then you must place them all within a widget, and that last widget set it in the QScrollArea. For example in your case:
// ...
QWidget *container = new QWidget;
scroll->setWidget(container);
QVBoxLayout *lay = new QVBoxLayout(container);
for(size_t i=0;i <tempList.size(); i++){
ProductWidget *p = new ProductWidget(...);
lay->addWidget(p);
}
// ...
I'm learning to use Qt and I want to extend the Terminal Example of Qt. I want to use its console.cpp in a QWidget from de Containers tab in the Design editor.
In the Terminal Example of Qt, this class is used like this:
ui->setupUi(this);
console = new Console;
console->setEnabled(false);
setCentralWidget(console);
But as I want to use it in a smaller QWidget I don't know how to set it, which method can I use as equivalent of setCentralWidget for my QWidget? Image of the Design tab with the widget I want to set to the QWidget class
Can I also use the same QWidget in several tabs?
The console.cpp code is the following one.
#include "console.h"
#include <QScrollBar>
#include <QtCore/QDebug>
Console::Console(QWidget *parent)
: QPlainTextEdit(parent)
, localEchoEnabled(false)
{
document()->setMaximumBlockCount(100);
QPalette p = palette();
p.setColor(QPalette::Base, Qt::black);
p.setColor(QPalette::Text, Qt::green);
setPalette(p);
}
void Console::putData(const QByteArray &data)
{
insertPlainText(QString(data));
QScrollBar *bar = verticalScrollBar();
bar->setValue(bar->maximum());
}
void Console::setLocalEchoEnabled(bool set)
{
localEchoEnabled = set;
}
void Console::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
case Qt::Key_Up:
case Qt::Key_Down:
break;
default:
if (localEchoEnabled)
QPlainTextEdit::keyPressEvent(e);
emit getData(e->text().toLocal8Bit());
}
}
void Console::mousePressEvent(QMouseEvent *e)
{
Q_UNUSED(e)
setFocus();
}
void Console::mouseDoubleClickEvent(QMouseEvent *e)
{
Q_UNUSED(e)
}
void Console::contextMenuEvent(QContextMenuEvent *e)
{
Q_UNUSED(e)
}
The Qt Example is this one: http://doc.qt.io/qt-5/qtserialport-terminal-example.html
Thanks so much!
If you're wanting to add it via designer just promote the QWidget that you added in your screegrab. (Right click > "Promote to..." > Fill in name & path to the console header).
Or not using promotion, you can add the console to a layout in code :
Console* console = new Console();
ui->your_layout_name_here->addWidget( console );
I'm trying get all the button child widgets of a window. The buttons were created through QDialogButtonBox. How do I get the which one is the cancel/ok/save button?
I have:
QWidget *pWin = QApplication::activeWindow();
QList<QPushButton *> allPButtons = pWin->findChildren<QPushButton *>();
QListIterator<QPushButton*> i(allPButtons);
while( i.hasNext() )
{
//identify which button is cancel/ok/save button here
/*Note: This is where I'm having trouble, getting the text of the
button here returns NULL. Any other way of Identifying which is
which?
Is it a special case when buttons are created through QDialogButtonBox?
*/
}
You should use the QDialogButtonBox::button() method, to get the button of the corresponding role.
For instance :
QPushButton* pOkButton = pButtonBox->button(QDialogButtonBox::Ok);
QPushButton* pCancelButton = pButtonBox->button(QDialogButtonBox::Cancel);
// and so on...
Generally speaking, I would say it's a bad idea to find a button from it's text, as this text might change when you app is internationalized.
One way is by text parameter from constructor such as QPushButton(const QString & text, QWidget * parent = 0):
QPushButton* buttonSave = new QPushButton("Save");
// etc..
while( i.hasNext() )
{
//identify which button is cancel/ok/save button here
if(i.next()->text() == "Save") {
// do something to save push button
}
}
Another alternative solution:
Use QDialogButtonBox's buttons function to return a list of all the buttons that have been added to the button box, then iterate over the list and identify each button using QDialogButtonBox's standardButton.
auto buttons = ui->buttonBox->buttons();
for (auto button : buttons) {
switch (ui->buttonBox->standardButton(button)) {
case QDialogButtonBox::Ok:
// do something
break;
case QDialogButtonBox::Cancel:
// do something
break;
case QDialogButtonBox::Save:
// do something
break;
default:
break;
}
}
For non-standard buttons
Map each button to an enum:
.h file:
private:
enum class NonStandardButton { Undo, Redo };
// map of non-standard buttons to enum class NonStandardButton
QHash<QAbstractButton*, NonStandardButton> buttonsMap;
.cpp file:
// in constructor
auto undoQPushButton =
ui->buttonBox->addButton("Undo", QDialogButtonBox::ActionRole);
auto redoQPushButton =
ui->buttonBox->addButton("Redo", QDialogButtonBox::ActionRole);
buttonsMap.insert(undoQPushButton, NonStandardButton::Undo);
buttonsMap.insert(redoQPushButton, NonStandardButton::Redo);
Then use QDialogButtonBox::NoButton to identify non-standard buttons:
switch (ui->buttonBox->standardButton(button)) {
case QDialogButtonBox::NoButton:
switch (buttonsMap.value(button)) {
case NonStandardButton::Undo:
// do something
break;
case NonStandardButton::Redo:
// do something
break;
default:
break;
}
default:
break;
}
I have a a QWidgetAction which holds a QWidget composed of a QLineEdit and a QPushButton. Once the user press the button the QWidgetAction call the trigger slot.
Now I have a QMenu which I activate with exec. The problem is that even though trigger is called (I've connected it to a print function as well to check) the menu won't close.
Regular QActions works well.
Any idea why?
P.S. Googling this issue I came across people with the same problem, but no solutions.
Years old question, but still I have an answer, hope it helps anybody!
I will describe my complete solution which not only hides the menu but also manages the visual representation.
QWidgetAction subclass: MyClass.h
class MyClass : public QWidgetAction {
Q_OBJECT
public:
MyClass(QObject* parent);
bool eventFilter(QObject*, QEvent*) override;
signals:
void mouseInside();
void mouseOutside();
protected:
QWidget* createWidget(QWidget* parent) override;
private:
QWidget* w;
QWidget* border;
QFormLayout *form;
QHBoxLayout *mainLayout;
}
QWidgetAction subclass MyClass.cpp
QWidget* MyClass::createWidget(QWidget* parent) {
w = new QWidget(parent);
border = new QWidget(parent);
mainLayout = new QHBoxLayout(w);
layout = new QFormLayout();
border->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum));
border->setMaximumWidth(10);
border->setMinimumWidth(10);
border->setMaximumHeight(1000); //Anything will do but it needs a height
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
w->setLayout(mainLayout);
mainLayout->addWidget(border);
mainLayout->addLayout(layout);
layout->setContentsMargins(6, 11, 11, 11);
layout->setSpacing(6);
// Insert your widgets here, I used a QFormLayout
QLineEdit *l = new QLineEdit(w);
form->addRow("Test", l);
// I added a button to accept input
QPushButton* b = new QPushButton("Send", w);
connect(b, SIGNAL(clicked()), this, SLOT(trigger()));
layout->addWidget(b);
w->installEventFilter(this); // This is to avoid non button clicks to close the menu
return w;
}
bool MyClass::eventFilter(QObject*, QEvent* ev) {
if (ev->type() == QEvent::MouseButtonPress
|| ev->type() == QEvent::MouseButtonDblClick
|| ev->type() == QEvent::MouseButtonRelease) {
return true;
} else if (ev->type() == QEvent::Enter) {
border->setStyleSheet("background-color: #90c8f6;");
emit mouseInside();
} else if (ev->type() == QEvent::Leave) {
border->setStyleSheet("");
emit mouseOutside();
}
return false;
}
Finally to insert the QWidgetAction in a menu, in your code add the following:
QMenu *m = new QMenu(this);
MyClass *item = new MyClass(m);
connect(item, &QAction::triggered, [=] { m->hide(); YOUR CODE HERE}); // Add your action here
// This is to give a visual cue to your item, while deselecting the stuck
// action which was previously selected
connect(item, &MyClass::mouseInside, [=] { m->setActiveAction(nullptr); });
ui->yourButton->setMenu(m);
m->addAction(item);