I have a project that I want to add button dynamically wherever I click in my form.
This is my header:
namespace Ui {
class frmBedBook;
}
class frmBedBook : public QWidget
{
Q_OBJECT
public:
explicit frmBedBook(QWidget *parent = 0);
void mousePressEvent(QMouseEvent *event);
~frmBedBook();
private:
Ui::frmBedBook *ui;
QSignalMapper *signalMapper;
QList<QPushButton*> buttonList;
QGridLayout *lyWidget;
QWidget *m_widget;
public slots:
void clicked(int buttonId);
};
And this is my implementation:
frmBedBook::frmBedBook(QWidget *parent) :
QWidget(parent),
ui(new Ui::frmBedBook)
{
ui->setupUi(this);
signalMapper = new QSignalMapper();
QPushButton *p;
lyWidget = new QGridLayout();
m_widget = new QWidget();
m_widget->setGeometry(0,0,930,472);
lyWidget->setContentsMargins(0,0,0,0);
lyWidget->addWidget(m_widget);
setLayout(lyWidget);
p = new QPushButton(m_widget);
p->setText("00");
p->setGeometry(0, 0, 50, 50);
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,0);
p = new QPushButton(m_widget);
p->setText("01");
p->setGeometry(50, 0, 50, 50);
p->setObjectName("01");
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,1);
connect(signalMapper, SIGNAL(mapped(int)),this, SLOT(clicked(int)));
}
void frmBedBook::mousePressEvent(QMouseEvent *event)
{
QPushButton *p;
p = new QPushButton(m_widget);
p->setText("02");
p->setGeometry(QCursor::pos().x(), QCursor::pos().y(), 50, 50);
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,2);
}
The problem is the button is created, but not visible. I know it because I have traced through m_widget's children and it's found. I also already resetting layout in MousePressEvent function, but nothing happened. Could anyone please help me about this problem?
You need to call show() on your buttons if they are added after the form has been constructed. Also, QCursor::pos() will probably not deliver the position you want to have.
You can use the x()/y() functions of the QMouseEvent instead:
void frmBedBook::mousePressEvent(QMouseEvent *event)
{
QPushButton *p;
p = new QPushButton(m_widget);
p->setText("02");
p->setGeometry(event->x(), event->y(), 50, 50);
p->show();
buttonList.append(p);
connect(p, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(p,2);
}
Note that you need to #include <QtGui/QMouseEvent> unless you already have that.
You never show the buttons, so they stay hidden. Use QWidget::show(). This applies to widgets created in mousePressEvent, after parent is already shown. The button created in constructor should get automatically shown when its parent is shown.
Related
I'm using the QGridLayout in my code, and want to add my custom widget to gridlayout, it's not working with addWidget(CustomWidget*).
This is runing on Windows10 with Visual Studio 2013 and Qt5.6.3.
// *.h
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
private:
Ui::MainWindow ui;
};
class CustomWidget : public QWidget {
Q_OBJECT
public:
CustomWidget(QWidget *parent = Q_NULLPTR) : QWidget(parent) {}
~CustomWidget() {}
};
// *.cpp
// when i use CustomWidget
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
ui.setupUi(this);
QWidget *grid = new QWidget(this);
grid->setStyleSheet("background:pink;");
QGridLayout *layout = new QGridLayout(grid);
layout->setMargin(0);
layout->setSpacing(0);
grid->setLayout(layout);
grid->setGeometry(500, 150, 240, 180);
// following code is not working, when run this program,
// i can only see the 'grid' widget with pink background
CustomWidget *w = new CustomWidget(grid);
w->setStyleSheet("background:red;");
layout->addWidget(w, 0, 0);
}
// but if i use QWidget
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
ui.setupUi(this);
QWidget *grid = new QWidget(this);
grid->setStyleSheet("background:pink;");
QGridLayout *layout = new QGridLayout(grid);
layout->setMargin(0);
layout->setSpacing(0);
grid->setLayout(layout);
grid->setGeometry(500, 150, 240, 180);
// following code is working, when run this program,
// i can see the 'w' widget with red background
QWidget *w = new QWidget(grid);
w->setStyleSheet("background:red;");
layout->addWidget(w, 0, 0);
}
As it is said in the Qt's stylesheets reference, applying CSS styles to custom widgets inherited from QWidget requires reimplementing paintEvent() in that way:
void CustomWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
Although for what it says in the documentation, without doing it your custom widgets will support only the background, background-clip and background-origin properties, for what might be a bug.
You can read about it here: Qt Stylesheets reference in the section "List of Stylable Widgets" -> QWidget.
I need to create a login dialog like this
image files: eyeOn.png, eyeOff.png
Requirements:
password is shown only when we CLICK AND HOLD the eyeOn icon (even when we click and hold and drag the mouse to area outside of the dialog, the password is still shown), when we release the mouse, password is covered again.
while password is shown, the eye icon is eyeOn. While password is covered, the eye icon is eyeOff.
I have just built the layout.
QGridLayout *mainlogin = new QGridLayout();
QLabel *usernameLabel = new QLabel;
usernameLabel->setWordWrap(true);
usernameLabel->setText("Username");
mainlogin->addWidget(usernameLabel, 0, 0);
QComboBox *usernameLineEdit = new QComboBox;
usernameLineEdit->setEditable(true);
usernameLabel->setBuddy(usernameLineEdit);
mainlogin->addWidget(usernameLineEdit, 0, 1);
QLabel *capslockShow = new QLabel;
capslockShow->setWordWrap(true);
capslockShow->setText(" ");
mainlogin->addWidget(capslockShow, 1, 1);
QLabel *passwordLabel = new QLabel;
passwordLabel->setWordWrap(true);
passwordLabel->setText("Password");
mainlogin->addWidget(passwordLabel, 2, 0);
QLineEdit *passwordLineEdit = new QLineEdit;
passwordLineEdit->setEchoMode(QLineEdit::Password);
QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);
passwordLabel->setBuddy(passwordLineEdit);
mainlogin->addWidget(passwordLineEdit, 2, 1);
What should I do next? Pls help me with code snippet.
The solution is to add a QAction to the QLineEdit, this will create a QToolButton that we can obtain from the associatedWidgets() (it will be the second widget since the first one is the one associated with clearButton). Already having the QToolButton you must use the pressed and released signal.
passwordlineedit.h
#ifndef PASSWORDLINEEDIT_H
#define PASSWORDLINEEDIT_H
#include <QAction>
#include <QLineEdit>
#include <QToolButton>
class PasswordLineEdit: public QLineEdit
{
public:
PasswordLineEdit(QWidget *parent=nullptr);
private slots:
void onPressed();
void onReleased();
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event);
private:
QToolButton *button;
};
#endif // PASSWORDLINEEDIT_H
passwordlineedit.cpp
#include "passwordlineedit.h"
PasswordLineEdit::PasswordLineEdit(QWidget *parent):
QLineEdit(parent)
{
setEchoMode(QLineEdit::Password);
QAction *action = addAction(QIcon(":/eyeOff"), QLineEdit::TrailingPosition);
button = qobject_cast<QToolButton *>(action->associatedWidgets().last());
button->hide();
button->setCursor(QCursor(Qt::PointingHandCursor));
connect(button, &QToolButton::pressed, this, &PasswordLineEdit::onPressed);
connect(button, &QToolButton::released, this, &PasswordLineEdit::onReleased);
}
void PasswordLineEdit::onPressed(){
QToolButton *button = qobject_cast<QToolButton *>(sender());
button->setIcon(QIcon(":/eyeOn"));
setEchoMode(QLineEdit::Normal);
}
void PasswordLineEdit::onReleased(){
QToolButton *button = qobject_cast<QToolButton *>(sender());
button->setIcon(QIcon(":/eyeOff"));
setEchoMode(QLineEdit::Password);
}
void PasswordLineEdit::enterEvent(QEvent *event){
button->show();
QLineEdit::enterEvent(event);
}
void PasswordLineEdit::leaveEvent(QEvent *event){
button->hide();
QLineEdit::leaveEvent(event);
}
void PasswordLineEdit::focusInEvent(QFocusEvent *event){
button->show();
QLineEdit::focusInEvent(event);
}
void PasswordLineEdit::focusOutEvent(QFocusEvent *event){
button->hide();
QLineEdit::focusOutEvent(event);
}
The complete example can be downloaded from the following link.
Rather than trying to make use of the QAction returned by QLineEdit::addAction you could probably use a QWidgetAction for this when combined with a suitable event filter...
class eye_spy: public QWidgetAction {
using super = QWidgetAction;
public:
explicit eye_spy (QLineEdit *control, QWidget *parent = nullptr)
: super(parent)
, m_control(control)
, m_on(":/eyeOn")
, m_off(":/eyeOff")
, m_pixmap_size(50, 50)
{
m_label.setScaledContents(true);
m_control->setEchoMode(QLineEdit::Password);
m_label.setPixmap(m_off.pixmap(m_pixmap_size));
m_label.installEventFilter(this);
setDefaultWidget(&m_label);
}
protected:
virtual bool eventFilter (QObject *obj, QEvent *event) override
{
if (event->type() == QEvent::MouseButtonPress) {
m_control->setEchoMode(QLineEdit::Normal);
m_label.setPixmap(m_on.pixmap(m_pixmap_size));
} else if (event->type() == QEvent::MouseButtonRelease) {
m_control->setEchoMode(QLineEdit::Password);
m_label.setPixmap(m_off.pixmap(m_pixmap_size));
}
return(super::eventFilter(obj, event));
}
private:
QLineEdit *m_control;
QLabel m_label;
QIcon m_on;
QIcon m_off;
QSize m_pixmap_size;
};
Now, rather than...
QAction *myAction = passwordLineEdit->addAction(QIcon(":/eyeOff.png"), QLineEdit::TrailingPosition);
Use...
eye_spy eye_spy(passwordLineEdit);
passwordLineEdit->addAction(&eye_spy, QLineEdit::TrailingPosition);
Seems to provide the desired behaviour.
I have simple class that inherits QDialog, I add dynamically elements
and my elements are located in the center, but I want to add them at the top.
class CustomDialog : public QDialog {
Q_OBJECT
private:
QVBoxLayout *mainLayout;
CustomDialog()
{
mainLayout = new QVBoxLayout();
setLayout(mainLayout);
}
public:
void update()
{
QLabel* label = new QLabel("some text");
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->addLayout(verLayout, Qt::AlignTop);
}
};
What am I doing wrong? and why my dynamically added elements always in center?
I understand that you want to place it and that the top is shown, so you could use QSpacerItem to push it.
class CustomDialog : public QDialog {
Q_OBJECT
QVBoxLayout *mainLayout;
public:
CustomDialog(QWidget *parent=0): QDialog(parent)
{
mainLayout = new QVBoxLayout(this);
QSpacerItem *verticalSpacer = new QSpacerItem(20, 217, QSizePolicy::Minimum, QSizePolicy::Expanding);
mainLayout->addItem(verticalSpacer);
addWidgets("1");
addWidgets("2");
}
private:
void addWidgets(const QString &text)
{
QLabel* label = new QLabel(text);
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->insertLayout(mainLayout->count()-1, verLayout);
}
};
Or if you want it to have a reverse order you must insert it in the first position with:
mainLayout->insertLayout(0, verLayout);
Note: The use of addLayout is incorrect since the second parameter is stretch.
My purpose is to use a class called overlay.h to add a rectangular box and text on top of a Widget (MarbleWidget). Here below is my code for the GUI. I tried to remove all unnecessary parts:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
overlay->resize(event->size()); //////////////// CAUSES SIGSEGV!!!!!!!!!!!!!
event->accept();
}
void MainWindow::setupUi(QMainWindow *MainWindow)
{
MainWindow->showMaximized();
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
MainWindow->setSizePolicy(sizePolicy);
MainWindow->setTabShape(QTabWidget::Rounded);
QWidget *centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
sizePolicy.setHeightForWidth(centralwidget->sizePolicy().hasHeightForWidth());
centralwidget->setSizePolicy(sizePolicy);
centralwidget->setLayoutDirection(Qt::LeftToRight);
QGridLayout *gridLayout_4 = new QGridLayout(centralwidget);
gridLayout_4->setObjectName(QString::fromUtf8("gridLayout_4"));
QSplitter *splitter_4 = new QSplitter(centralwidget);
splitter_4->setObjectName(QString::fromUtf8("splitter_4"));
splitter_4->setOrientation(Qt::Horizontal);
QTabWidget *tabWidget_2 = new QTabWidget(splitter_4);
tabWidget_2->setObjectName(QString::fromUtf8("tabWidget_2"));
sizePolicy.setHeightForWidth(tabWidget_2->sizePolicy().hasHeightForWidth());
tabWidget_2->setSizePolicy(sizePolicy);
tabWidget_2->setMaximumSize(QSize(443, 16777));
tabWidget_2->setAutoFillBackground(true);
tabWidget_2->setTabPosition(QTabWidget::West);
tabWidget_2->setTabShape(QTabWidget::Rounded);
tabWidget_2->setIconSize(QSize(30, 16));
QWidget *tab_10 = new QWidget();
tab_10->setObjectName(QString::fromUtf8("tab_10"));
QGridLayout *gridLayout = new QGridLayout(tab_10);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
QVBoxLayout *verticalLayout_4 = new QVBoxLayout();
verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));
QHBoxLayout *horizontalLayout_7 = new QHBoxLayout();
horizontalLayout_7->setObjectName(QString::fromUtf8("horizontalLayout_7"));
QLabel *label_3 = new QLabel(tab_10);
label_3->setObjectName(QString::fromUtf8("label_3"));
horizontalLayout_7->addWidget(label_3);
verticalLayout_4->addLayout(horizontalLayout_7);
QTreeView *treeView_4 = new QTreeView(tab_10);
treeView_4->setObjectName(QString::fromUtf8("treeView_4"));
QStandardItemModel *standardModel = new QStandardItemModel ;
QStandardItem *rootNode = standardModel->invisibleRootItem();
treeView_4->setModel(standardModel);
verticalLayout_4->addWidget(treeView_4);
gridLayout->addLayout(verticalLayout_4, 0, 0, 1, 1);
tabWidget_2->addTab(tab_10, QString());
treeView_4->raise();
Marble::MarbleWidget* MarbleWidget = new Marble::MarbleWidget(splitter_4);
splitter_4->addWidget(MarbleWidget);
splitter_4->setStretchFactor(0,0);
splitter_4->setStretchFactor(1,6);
gridLayout_4->addWidget(splitter_4, 0, 0, 1, 1);
MainWindow->setCentralWidget(centralwidget);
MarbleWidget->raise();
tabWidget_2->raise();
overlay = new Overlay(MarbleWidget);
overlay->raise();
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
overlay.h
#include <QWidget>
#include <QPainter>
class Overlay : public QWidget
{
public:
Overlay(QWidget *parent);
protected:
void paintEvent(QPaintEvent *event);
};
overlay.cpp
#include "overlay.h"
Overlay::Overlay(QWidget *parent)
: QWidget(parent)
{
setPalette(Qt::transparent);
setAttribute(Qt::WA_TransparentForMouseEvents);
}
void Overlay::paintEvent(QPaintEvent *event)
{
QFont font;
font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);
font.setPointSize(10);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QColor(10, 10, 10, 255));
painter.fillRect(QRect(50, 50, 100, 100), QColor(100, 100, 100, 120));
painter.setFont(font);
painter.drawText(20, 20, "hi..............................");
}
The problem is that overlay->resize(event->size()); causes SIGSEGV when the core runs that line.
What is wrong with the code, how can I fix it?
The problem is solved by Thiago from freenode.
He pointed that MainWindow->showMaximized(); causes resize() event to be occurred before overlay is initialized. Removing that line or moving it to after the initialization solves the problem.
Take a look at official documentation: QWidget, Qt5 at resize method. Here you can read
Warning: Calling resize() or setGeometry() inside resizeEvent() can lead to infinite recursion.
You are doing exactly the same. To avoid infinite recursion and respectively your SIGSEGV you can (descending order of difficulty, descending order of true-way):
overwrite your resizeEvent() in Overlay and comment that line in MainWindow's resizeEvent;
in MainWindow's resizeEvent emit signal like mainWindowSizeChanged(QSize) and connect it to your Overlay's slot;
do QTimer::singleShot(...) in MainWindow's resizeEvent, so it will call a given slot after a given time interval.
I'm trying to connect a button click to a function, and pass an int value, but keep getting an error: no matching function for call to Main::connect... I'm guessing that I'm not initializing something correct?
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setupUI();
}
MainWindow::~MainWindow() {
}
void MainWindow::setupUI() {
QFrame* frame = new QFrame(this);
_layout = new QVBoxLayout;
frame->setLayout(_layout);
parseXML();
QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(frame);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
}
void MainWindow::parseXML() {
this->parseItem(xml, count)
}
QMap<QString, QString> MainWindow::parseItem(QXmlStreamReader& xml, int count) {
QString valueName = "buttonid";
QSignalMapper *signalMapper = new QSignalMapper(this);
QPushButton* button = new QPushButton(valueName);
_layout->addWidget(button);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(addMenu(int)));
signalMapper->setMapping(button, valueName);
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
return something;
}
void MainWindow::addMenu(int count) {
_layoutToAdd = new QVBoxLayout;
QPushButton* button = new QPushButton("New Button");
_layoutToAdd->addWidget(button);
_layout->insertLayout(count, _layoutToAdd, 0);
}
mainwindow.h
ublic:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void parseXML();
void addMenu(int count);
signals:
void clicked();
There's a syntax error in your code. Count your parenthesis :
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
// ^ ^ ^^^^
// 1 2 3321 <-- All your parenthesis are closed at this point
// meaning you are calling connect() with only 2 parameters
You must white Q_OBJECT (for using signals and slots ) in descriction of your class
class MainWindow: public QObject
{
Q_OBJECT