I am attempting to add an item to a QListWidget, select that item, and then begin editing the new item.
The item gets added, and it gets selected, but the line is not brought into a QLineEdit, or whatever it would attempt to use.
Here's the code for my widget, with the relevant code under the connect for "m_addButton":
Sequencer::Sequencer(QWidget* parent) :
QWidget(parent)
{
m_list = new QListWidget();
m_addButton = new QPushButton("Add Step");
m_removeButton = new QPushButton("Remove Step");
connect(m_addButton, &QPushButton::clicked, this, [&]()
{
m_list->addItem(""); // This part works,
m_list->setCurrentItem(m_list->item(m_list->count() - 1)); // This part works...
m_list->editItem(m_list->currentItem()); // This part puts out "edit: editing failed" into the console.
});
connect(m_removeButton, &QPushButton::clicked, this, [&]()
{
if (!m_list->selectedItems().isEmpty())
{
qDeleteAll(m_list->selectedItems());
}
});
QHBoxLayout* hLayout = new QHBoxLayout();
hLayout->addStretch();
hLayout->addWidget(m_addButton);
hLayout->addWidget(m_removeButton);
QVBoxLayout* vLayout = new QVBoxLayout();
vLayout->addWidget(m_list);
vLayout->addLayout(hLayout);
setLayout(vLayout);
}
By default a QListWidgetItem is not editable so if you want it to be, you must enable the Qt::ItemIsEditable flag:
connect(m_addButton, &QPushButton::clicked, this, [&]()
{
QListWidgetItem *item = new QListWidgetItem;
item->setFlags(item->flags() | Qt::ItemIsEditable); # enable
m_list->addItem(item);
m_list->setCurrentItem(item);
m_list->editItem(item);
});
Related
I'm writing a qt program and trying to resize mainwindow when sub widget is hidden, but there's some difference in Linux and Windows, and I don't know the property way to adjust size in both.
Here's my minimum code:
// MainWindow
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *mainWidget = new QWidget(this);
QVBoxLayout *mainlayout = new QVBoxLayout(mainWidget);
QPushButton *hide = new QPushButton(mainWidget);
subwidget *sub = new subwidget(mainWidget);
mainlayout->addWidget(hide);
mainlayout->addWidget(sub);
connect(hide, &QPushButton::clicked, sub, &subwidget::onHide);
setCentralWidget(mainWidget);
adjustSize();
connect(hide, &QPushButton::clicked, this, [&] {
QTimer::singleShot(0, this, [&] { adjustSize(); });
});
}
// Widget
subwidget::subwidget(QWidget *parent)
: QWidget{parent}
{
QVBoxLayout *vlayout = new QVBoxLayout(this);
QPushButton *btn1 = new QPushButton();
btn2 = new QPushButton();
vlayout->addWidget(btn1);
vlayout->addWidget(btn2);
}
void subwidget::onHide()
{
btn2->hide();
}
When I set the timer interval to 1, mainwindow will be the adjusted size when btn2 is hidden in Linux, but it doesn't work in Windows. How can I adjust the mainwindow in windows?
PS: I have tried to add a signal when subwidget is hidden and connect it to adjustSize like connect(sub, &subwidget::hidden, this, [&] { adjustSize(); });, but it also doesn't work.
please, help me to solve this probleme of qt;
QWidget::setLayout: Attempting to set QLayout "" on Login_1 "Login_1", which already has a layout ?
Login_1::Login_1(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Login_1)
{
ui->setupUi(this);
//The main windows
QGridLayout* MainLayout = new QGridLayout();
//The first ligne (username, line 0)
QLabel* LbNom = new QLabel("User name");
QLineEdit* LeNom = new QLineEdit();
MainLayout->addWidget(LbNom,0,0);
MainLayout->addWidget(LeNom,0,1);
//The second line (password, line 1)
QLabel* LbPassword = new QLabel("Password");
QLineEdit* LePassword = new QLineEdit(this);
MainLayout->addWidget(LbPassword,1,0);
MainLayout->addWidget(LePassword,1,1);
//Login Button(line 2)
QPushButton* PbLogin = new QPushButton(this);
PbLogin->setText("Login");
MainLayout->addWidget(PbLogin,2,0);
//setLayout(MainLayout);
}
Login_1::~Login_1()
{
delete ui;
}
Thank you for your helping...
I create at first time a new widget:
QWidget* Mywidget = new QWidget();
After tha, I make for this widget (Mywidget) parent of my odd layout:
QGridLayout* MainLayout = new QGridLayout(Mywidget);
And it work
I am trying to make a panel that shows some data, that gets added when I press a button. I will explain it trough these images:
this would be the initial state of the app, a window with a QGraphicsView
if I click "Help" it should display a window above it that never goes out of focus
I looked into using QDockWidget, but that just creates a panel next to it, that that's not what I Want. If anyone knows how to do this, I would be very grateful, thanks.
You can set children widgets in your QGraphicsView and consider it like a regular QWidget:
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
QGraphicsView* view = new QGraphicsView(scene);
view->show();
QPushButton* button = new QPushButton("Show label");
QLabel* label = new QLabel("Foobar");
QVBoxLayout* layout = new QVBoxLayout(view);
layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
layout->addWidget(button);
layout->addWidget(label);
label->hide();
QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
return app.exec();
The label will be visible in the QGraphicsView when you click on the button.
You can also embed a widget in your scene with QGraphicsProxyWidget class:
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene(0, 0, 1000, 1000);
scene->addItem(new QGraphicsRectItem(500, 500, 50, 50));
QGraphicsView* view = new QGraphicsView(scene);
view->show();
QWidget* w = new QWidget();
QGraphicsProxyWidget* proxy = new QGraphicsProxyWidget();
QPushButton* button = new QPushButton("Show label");
QLabel* label = new QLabel("Foobar");
QVBoxLayout* layout = new QVBoxLayout(w);
layout->addWidget(button);
layout->addWidget(label);
layout->setAlignment(Qt::AlignRight | Qt::AlignTop);
label->hide();
QObject::connect(button, &QPushButton::clicked, label, &QLabel::show);
proxy->setWidget(w);
scene->addItem(proxy);
return app.exec();
I have the following code in the contructor of my main window, which extends QMainWindow class:
QHBoxLayout* mainLayout = new QHBoxLayout(this);
QPushButton* button0 = new QPushButton(this);
button0->setText("Button 0");
mainLayout->addWidget(button0);
QPushButton* button1 = new QPushButton(this);
button1->setText("Button 1");
button1->setMinimumWidth(500);
mainLayout->addWidget(button1);
QPushButton* button2 = new QPushButton(this);
button2->setText("Button 2");
mainLayout->addWidget(button2);
setMinimumSize(700,480);
this->setLayout(mainLayout);
However, when I tried to run this, the buttons seem to be overlapping on the top left corner. What should I change here?
Check the console output. Usually main windows do have layout by default, then you just need to set it on the central widget instead of the window itself:
this->centralWidget()->setLayout(mainLayout);
If central widget does not exist (for example you deleted auto-generated .ui file) then just create it manually:
QWidget* centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QHBoxLayout* mainLayout = new QHBoxLayout(centralWidget);
QPushButton* button0 = new QPushButton(this);
button0->setText("Button 0");
mainLayout->addWidget(button0);
QPushButton* button1 = new QPushButton(this);
button1->setText("Button 1");
button1->setMinimumWidth(500);
mainLayout->addWidget(button1);
QPushButton* button2 = new QPushButton(this);
button2->setText("Button 2");
mainLayout->addWidget(button2);
setMinimumSize(700,480);
How can i add different layouts under QStackedWidget. I have 3 QVBoxLayout and i want to add this QVBoxLayout under QStackedWidget. I know layout cant add inside widget. but is there is any way to do that. So that from QStackedWidget i can change different layout.
mywindow::mywindow() : QMainWindow()
{
centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
layout1 = new QVBoxLayout(centralWidget);
layout2 = new QVBoxLayout();
layout3 = new QVBoxLayout();
stack1 = new QStackedWidget();
list1 = new QListWidget();
list2 = new QListWidget();
list3 = new QListWidget();
label1 = new QLabel("Main Menu");
label2 = new QLabel();
label3 = new QLabel("Hello");
label4 = new QLabel("Hi");
line1 = new QLineEdit();
list1->addItem("Item 1");
list1->addItem("Item 2");
list1->addItem("Item 100");
list2->addItem("Item 3");
list2->addItem("Item 4");
list3->addItem("Item 5");
list3->addItem("Item 6");
layout1->addWidget(label1);
layout1->addWidget(list1);
layout1->addWidget(label2);
layout2->addWidget(label3);
layout2->addWidget(list2);
layout3->addWidget(label4);
layout3->addWidget(list3);
stack1->addWidget(layout1);
stack1->addWidget(layout2);
stack1->addWidget(layout3);
this->setLayout(layout);
}
Create 3 QWidget objects, add each layout to each of the widgets and then add the widgets to the stacked widget.