C++/Qt Problem with QLineEdit: Cannot get text - c++

I am trying to input an API key from the user but the string is always empty. The code is as follows:
QObject::connect(ui->apiKeySubmit, SIGNAL(clicked()), this, SLOT(getApikey()));
void MainWindow::getApiKey() {
key = ui->apiEdit->text();
}
key is a QString and a private member of the class MainWindow
//I always get this message
if(key == "") {
QMessageBox* message = new QMessageBox(this);
message->setText("Please enter an API key");
message->exec();
}
multipart->append(partParameter("apikey",key));
And the part of mainwindow.ui with those widgets is as follows:
<widget class="QLineEdit" name="apiEdit">
<property name="geometry">
<rect>
<x>160</x>
<y>60</y>
<width>181</width>
<height>26</height>
</rect>
</property>
<property name="inputMask">
<string notr="true"/>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
<widget class="QPushButton" name="apiKeySubmit">
<property name="geometry">
<rect>
<x>160</x>
<y>90</y>
<width>181</width>
<height>26</height>
</rect>
</property>
<property name="text">
<string>Submit</string>
</property>
</widget>

I had a typo, getApikey() instead of getApiKey(), should've looked at the code more carefully before posting the question.

Related

How to send data from C++ to .UI file in Qt?

I'm currently making an embedded system using an ultrasonic sensor. In QT, I want to get data from the sensor, and send it to a .ui file. How can I send the data?
For example:
Sensor.cpp
int main(int argc, char **argv){
while(1)
unsigned int result = (high <<8) + low;
printf("Range : %u\n",result);
sleep(1)
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTextBrowser" name="sensor_ourput">
<property name="geometry">
<rect>
<x>280</x>
<y>150</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
I want to put the sensor data from result variable to sensor_ourput in mainwindow.ui.

How to made QGraphicsView fullscreen?

I have a QDialog window of size 800x480 and my sample image also have same size (800x480), But when it put it in QGraphicsView it showing a small space between image and window, how to avoid this space (yellow color )
I am using Qt5 in windows10
My code is
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
scene= new QGraphicsScene(this);
QPixmap pix(qApp->applicationDirPath()+QDir::separator()+"testImage.png");
pixItem = scene->addPixmap(pix);
ui->graphicsView->setScene(scene);
ui->graphicsView->showMaximized();
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::showEvent(QShowEvent *)
{
ui->graphicsView->fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio);
}
And my xml generated by qt designer is
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 0);</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustIgnored</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
I tried by adding image size bigger than 800x480 but the space is there
I used both QDialog and QMainwindow, but both of them have same problem with QGrapgicsView.
Finally I changed QGraphicsView with QLabel and it does not have this problem.

Why am I unable to delete tabWidget in Qt Designer?

I am using Qt Designer in combination with MSVC2019. I recently changed my form's base class from QWidget to QMainWindow manually by editing the .ui file, header, and constructor. In this form I have a tabWidget, which is now not able to be selected. It shows up in the object inspector and property editor, and I can edit its properties, but not select it. This is unlike all the other widgets in my form. When I right-click on the tabWidget in the Object inspector it gives the same menu options as a QMainWindow (Create Menu Bar, Add Tool Bar).
I have tried changing various object properties of the tabWidget and its main window but nothing has worked.
Here is the .ui region of interest. All I did was replace Widget with QMainWindow in line 4.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>campanel</class>
<widget class="QMainWindow" name="campanel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1509</width>
<height>984</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>FASTPLOTTER</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>bigmoves.ico</normaloff>bigmoves.ico</iconset>
</property>
<property name="windowOpacity">
<double>1.000000000000000</double>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="dockNestingEnabled">
<bool>true</bool>
</property>
<property name="dockOptions">
<set>QMainWindow::AllowNestedDocks|QMainWindow::AllowTabbedDocks|QMainWindow::AnimatedDocks|QMainWindow::ForceTabbedDocks</set>
</property>
<widget class="QTabWidget" name="tabWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<property name="documentMode">
<bool>true</bool>
</property>
<property name="tabsClosable">
<bool>true</bool>
</property>
<property name="movable">
<bool>true</bool>
</property>
<widget class="QWidget" name="iolji">
<attribute name="title">
<string/>
</attribute>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string/>
</attribute>
</widget>
</widget>
QMainWindow is a special case of QWidget: it has a menu bar, a status bar, tool bars, can add dockable frames, and have a central-widget. While you can switch between QWidget and QDialog in UI files, when switching to QMainWindow you must embed all the content of previous widget into the centralWidget:
<widget class="QMainWindow" name="MainWindow">
<!-- size policy, geometry... -->
<widget class="QWidget" name="centralwidget">
<!-- size policy, layout, content of your previous QWidget -->
</widget>
</widget>

QPushButton changes size of widgets in layout

This problem is driving me insane and I can't seem to find a logical answer. I'm pretty new to this so please bear with.
I've been creating an application where I create a vertical 'navigation bar' and need to add QPushButtons dynamically. I've noticed that the
horizontal position of a label in a QVBox changes when a QPushButton is added to it.
I've created a minimal version:
Before Adding QPushButton:
Label reaches the edge of the application
After adding QPushButton:
Label width is reduced slightly
Here is the code I'm using to dynamically add the QPushButton:
void MainWindow::on_pushButton_clicked()
{
QPushButton *newButton = new QPushButton("Test");
newButton->setContentsMargins(0,0,0,0);
newButton->setStyleSheet("margin: 0; padding: 0;");
ui->verticalLayout->setMargin(0);
ui->verticalLayout->setContentsMargins(0,0,0,0);
// add new push button inside VBox
ui->verticalLayout->addWidget(newButton);
}
.ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>203</width>
<height>224</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>201</width>
<height>151</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="styleSheet">
<string notr="true">border: 1px solid white;</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>180</y>
<width>181</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Add Push Button To VBox</string>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
As you can see setting the margins on both the QPushButton and the layout don't seem to have any effect. Is there anyone who could shed some light on this issue?
Let's analyze your .ui with the help of Qt Designer, if we stretch the window you get the following:
As you can see the layout only affects the QLabel, so the initial button will not be handled by the layout, but the added button will be. Therefore, you do not observe a similar behavior.
The solution is to restructure the design using the following structure:
QMainWindow
└── QVBoxLayout
├── QLabel
└── QWidget
└── QVBoxLayout
└── QPushButton
You must change the size policy of the QWidget to take the minimum height by setting Maximum in Vertical Policy:
And in that second layout add the button. The .ui is the following:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>335</width>
<height>303</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="styleSheet">
<string notr="true">border: 1px solid white;</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Add Push Button To VBox</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Then it is no longer necessary to modify the button:
void MainWindow::on_pushButton_clicked()
{
QPushButton *newButton = new QPushButton("Test");
ui->verticalLayout->addWidget(newButton);
}
Obtaining the following:

Setting QLabel pixmap in Qt creator design view

I've set the pixmap field of a QLabel to :/Resources/Logo.jpg in Qt creator design view. As a result the ui file looks like
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>301</width>
<height>171</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="Utility.qrc">:/Resources/Logo.jpg</pixmap>
</property>
</widget>
From which the following code is generated
label_4 = new QLabel(centralWidget);
label_4->setObjectName(QStringLiteral("label_4"));
label_4->setGeometry(QRect(10, 0, 301, 171));
label_4->setTextFormat(Qt::AutoText);
label_4->setPixmap(QPixmap(QString::fromUtf8(":/Resources/Logo.jpg")));
And although the pixmap is visible in design view it does not appear when I run the application. What am I doing wrong?