QTableWidget headers not displaying - qt

I use the UI editor to create a QTableWidget.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
populateFilesTable();
connect(ui->browseButton, SIGNAL(clicked()), this, SLOT(selectDirectory()));
connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(findFiles()));
}
This shows how the UI is setup and then I call the function populateFilesTable().
The function is as follows:
void MainWindow::populateFilesTable()
{
ui->filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
QStringList labels;
labels << tr("Filename") << tr("Size");
ui->filesTable->setHorizontalHeaderLabels(labels);
ui->filesTable->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
ui->filesTable->verticalHeader()->hide();
ui->filesTable->setShowGrid(true);
}
The headers aren't being displayed on the table, any ideas?
Thanks.

What's wrong?
The horizontal header needs the information of columns from QTableWidget. As a QTableWidget is instantiated, both column count and row count are null hence you got no headers show even if you called setHorizontalHeaderLabels.
Solution
Insert columns before you set the header:
ui->filesTable->insertColumn(0);
ui->filesTable->insertColumn(1);
QStringList labels;
labels << tr("Filename") << tr("Size");
ui->filesTable->setHorizontalHeaderLabels(labels);

For PyQt5,
check if it is self.tableWidget.horizontalHeader().setVisible(True) and not self.tableWidget.horizontalHeader().setVisible(False)
Idk why but after converting ui file to py file, the set this attribute as false by default.

Related

Can someone give me a simple qt example?

I want to implement a simple QT example: click the QPushButton to display a paragraph of text.
Like this:
I know there are many ways to implement it, but I don't know what's wrong with my code.
QPushButton *btn = new QPushButton;
//btn->show();
btn->setParent(this);
btn->setText("button 1");
QLabel *la = new QLabel(this);
connect(btn,&QPushButton::clicked,la,&QLabel::setText("show me"));
Anyone who can help me?
The idea is to create the QLabel with a predefined text and hide it. On clicking the button, show it. So, QLabel::hide and QLabel::show could be used here. Just take care of the coordinates where you show the label because it would overlap the button itself without layout or proper coordinates.
Example (mainwindow.cpp, constructor):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("My App"); // title bar text
QPushButton *btn = new QPushButton(this);
btn->setText("Click Me!");
QLabel *la = new QLabel(this);
la->setText("Show Me!"); // set label text in advance
la->move(100, 100); // set label position
la->hide(); // hide label on load
connect(btn, &QPushButton::clicked, la, &QLabel::show);
}
MainWindow::~MainWindow()
{
delete ui;
}
Output (after clicking the image):

Button is not showing on main window even after successful execution of code in Qt

I have tried this code but the button isn't displaying on the main window.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QGridLayout>
#include<QLabel>
#include<QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout();
q->addWidget(l);
this->setLayout(q);
this->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
I have tried to change the code even by passing enums for alignment but nothing worked.
When you create new Qt widgets Application, the default form (MainWindow ui) is created with centralWidget to put all other widgets. In your code you created the QGridLayout without a parent, typically such layout should be placed in ui->centralWidget (as far as you are not creating another widget to be set as centralWidget), moreover I assume your mainWindow is shown from main.cpp (need not use show()). your code could thus be:
QPushButton *l=new QPushButton();
l->setText("abc");
QGridLayout *q=new QGridLayout(ui->centralWidget);
q-> addWidget(l);
Try adding the widget to the GridLayout with index using addWidget function
void QGridLayout::addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = ...)
like:
q-> addWidget(l, 0, 0);
P.S. also consider using better names for your variables!

Change property of random element in QListWidget

I have 5 QProgressBars in a QListWidget (ui->listWidget). How can I access the third QProgressBar element and change its value ex. ( progressBar->setValue(40) )
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
a = new QPushButton(this);
connect(a, SIGNAL (clicked()),this, SLOT (clickedSlot()));
}
void MainWindow::clickedSlot()
{
QProgressBar *prog = new QProgressBar(this);
QListWidgetItem *it;
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->insertItem(ui->listWidget->size().height(),it);
it->setSizeHint(QSize(200,50));
ui->listWidget->setItemWidget(it, prog);
}
The following code will obtain the third element in the list and set the progress to 40%.
QProgressBar *bar = qobject_cast<QProgressBar*>(ui->listWidget->itemWidget(pList->item(2)));
if (bar)
bar->setValue(40);
qobject_cast will safely cast the QWidget to QProgressBar, only if the widget is indeed a QProgressBar. If you are sure the third element is a QProgressBar, you can omit the if test if(bar).
See the qt documentation QListWidget and qobject_cast for more information.
Create the definition for the QProgressBar(s) in the class header file, then you can connect things to the setValue slot, or access them directly.
It seems odd to be adding ProgressBars to QListWidgetItems... wouldn't QHBoxLayout be more suitable?

QDockWidget will not show contents

I am attempting to create a specialised QDockWidget, with some contents, that I can use in other Qt5 UIs. To do this, I have created a QDockWidget subclass complete with a UI form, called SpecialDockWidget. The contents (a QTreeWidget) are added in the UI form.
In my main UI, I have added SpecialDockWidget as a QDockWidget promotion, have promoted the desired dock widget, and everything compiles OK. However, when I run the application my promoted dock widget contains the title of the SpecialDockWidget in its title bar, but the contents remain empty. This only happens if the dock widget is made part of the main UI (whether floating or attached), and the dock widget displays the contents successfully if it is instantiated on its own as a top-level widget.
The Qt documentation says the following with regards to QDockWidget::setWidget():
If the dock widget is visible when widget is added, you must show() it explicitly.
Note that you must add the layout of the widget before you call this function; if not, the widget will not be visible.
I have tried calling show() on both the SpecialDockWidget and the contents, and have set the layout myself, but the contents still don't show up.
Can anyone tell me what I might be doing wrong?
EDIT: Some photos showing what's going on:
Dock widget code:
#include "specialdockwidget.h"
#include "ui_specialdockwidget.h"
#include <QVBoxLayout>
SpecialDockWidget::SpecialDockWidget(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::SpecialDockWidget)
{
ui->setupUi(this);
}
SpecialDockWidget::~SpecialDockWidget()
{
delete ui;
}
Main window code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// This produces the top-level dock widget
// SpecialDockWidget* w = new SpecialDockWidget();
// w->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The QDockWidget is created in the Designer so it's not very clear how the tree is added to the QDockWidget. If the tree is simply a child of the QDockWidget it's not correct. You must add the tree to the QDockWidget using QDockWidget::setWidget().
I suggest to not use the Designer to create a subclass of QDockWidget because it seems not possible to add the content correctly. You can do something like this instead.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dw = new QDockWidget(this);
QTreeWidget* tw = new QTreeWidget(dw);
dw->setWidget(tw);
addDockWidget(Qt::LeftDockWidgetArea, dw);
dw->show();
}
Of course, if you need to put inside a dock widget a more complex widget, you can create the content with the Designer as a simple QWidget subclass and add it to the dock widget in the same way.
I think your main window's constructor should look like, for example:
MainWindow::MainWindow(QWidget *parent)
:
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
SpecialDockWidget *w = new SpecialDockWidget();
// Add dock widget to the specified area of main window.
addDockWidget(Qt::LeftDockWidgetArea, w);
}

Set item for ComboBox of QT in function

I want to set item for comboBox of QT in function.
I coded below:
Function:
void addItemInComboBoxBaudRate(){
Ui::MainWindow::cbBaudRate->addItem("2400");
Ui::MainWindow::cbBaudRate->addItem("4800");
Ui::MainWindow::cbBaudRate->addItem("9600");
Ui::MainWindow::cbBaudRate->addItem("19200");
Ui::MainWindow::cbBaudRate->addItem("38400");
Ui::MainWindow::cbBaudRate->addItem("57600");
Ui::MainWindow::cbBaudRate->addItem("115200");
Ui::MainWindow::cbBaudRate->setCurrentText("9600)");
}
In main function:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
addItemInComboBoxBaudRate();
}
Error:
/home/san/workspace/build-dieu_khien_san-Desktop-Debug/ui_mainwindow.h:44: error: invalid use of non-static data member 'Ui_MainWindow::cbBaudRate'
QComboBox *cbBaudRate;
/home/san/workspace/dieu_khien_san/mainwindow.cpp:20: error: from this location
Ui::MainWindow::cbBaudRate->addItem("2400");
change the code into
void addItemInComboBoxBaudRate(){
Ui::MainWindow->cbBaudRate->addItem("2400");
Ui::MainWindow->cbBaudRate->addItem("4800");
Ui::MainWindow->cbBaudRate->addItem("9600");
Ui::MainWindow->cbBaudRate->addItem("19200");
Ui::MainWindow->cbBaudRate->addItem("38400");
Ui::MainWindow->cbBaudRate->addItem("57600");
Ui::MainWindow->cbBaudRate->addItem("115200");
Ui::MainWindow->cbBaudRate->setCurrentText("9600)");
}
edited: try that way (without the mainwindow.h code I'm just guessing), if it doesn't work, please edit your question with full header file (or a least all the MainWindow class)

Resources