Such like title I say,
The game.cpp:
#include "game.h"
#include "ui_game.h"
#include "qdebug.h"
Game::Game(QWidget *parent) :
QWidget(parent),
ui(new Ui::Game)
{
ui->setupUi(this);
QPalette palette;
QPixmap pixmap(":/bgimg");
palette.setBrush(this->backgroundRole(),QBrush(pixmap));
this->setPalette(palette);
InitLayout(6,6);
}
Game::~Game()
{
delete ui;
}
void Game::InitLayout(int widthNum, int heightNum)
{
ui->grid->setGeometry(QRect(0,0,1000,800));
for(int i=0; i< heightNum;i++)
{
for(int j=0;j<widthNum;j++)
{
int total = i*10+j;
QString s = QString::number(total);
qDebug() << s << "\n";
QLabel*item = new QLabel(this);
//item->setText(s);
ui->grid->addWidget(item,i+1,j+1);
}
}
}
And cell.cpp
#include "cell.h"
cell::cell(QWidget *parent):
QLabel(parent)
{
this->setGeometry(0,0,20,20);
QPixmap qpixmap(":/qizi");
this->setPixmap(qpixmap);
}
I want to show the cutting line int Grid(You know that grid are divided into many cell by line). I can't find to property in Grid.
Related
I'm using this simple code to try to understand why the size of the widget in the first row is changing while adding rows to the table but all other rows remain intact...
This is the simple code I'm trying:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QCheckBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList HorizontalHeaders;
HorizontalHeaders << "Number" << "Test";
ui->tableWidget->setColumnCount(2);
ui->tableWidget->setHorizontalHeaderLabels(HorizontalHeaders);
ui->tableWidget->horizontalHeader()->setVisible(true);
ui->tableWidget->verticalHeader()->setVisible(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_spinBox_valueChanged(int arg1)
{
ui->tableWidget->setRowCount(arg1);
ui->tableWidget->clear();
for(int i = 0; i < arg1; i++){
QWidget* thiswidget = new QWidget();
QHBoxLayout* thislayout = new QHBoxLayout(thiswidget);
QCheckBox* thischeckbox = new QCheckBox();
QLabel* thislabel = new QLabel(QString::number(i));
thislayout->addWidget(thischeckbox);
thislayout->addWidget(thislabel);
ui->tableWidget->setCellWidget(i, 0, thiswidget);
ui->tableWidget->setItem(i, 1, new QTableWidgetItem("Test " + QString::number(i)));
ui->tableWidget->setRowHeight(i, thiswidget->height());
}
}
the result with just 2 rows:
the result with many row:
I have written a function in Qt to extract the data out of an image for the purpose of manipulating it.
I then have another function to reinsert the data back to an image and display it. The problem I am having is that even if I do no manipulation on the pixel data other than extract and reinsert, it is still changing the data. On a yellow image it changes it to turquoise blue when it should remain yellow.
I am including the function code to extract and reinsert as specimen code. I can include more if it is needed such as the display function etc...Does anyone know if I am doing something wrong?
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
filter = "All Picture Files (*.png *.jpg *.jpeg *.bmp *.tif *.tiff)"
";; Bitmap Files (*.bmp) ;; JPEG (*.jpg *.jpeg) ;; PNG (*.png) ;; TIFF (*.tif *.tiff)";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::importImage()
{
importCancelled = false;
QString filename = QFileDialog::getOpenFileName(nullptr, QObject::tr("Import Image"), "", filter);
if(!filename.isEmpty()){
image.load(filename);
image = image.convertToFormat(QImage::Format_RGBA8888);
}
else {
importCancelled = true;
if(importCancelled){
QString cleanPlateCancelled = "Operation Cancelled";
ui->statusBar->showMessage(cleanPlateCancelled,5000);
return;
}
}
}
void MainWindow::scaleImage()
{
if (image.isNull()){
return;
}
else {
image = image.scaledToHeight(ui->view->height(), Qt::TransformationMode::SmoothTransformation);
}
}
void MainWindow::displayImage()
{
if (image.isNull()){
return;
}
else {
scene = new QGraphicsScene;
showImage = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(showImage);
ui->view->setScene(scene);
}
}
void MainWindow::rgbaExtraction()
{
numberOfBytes = static_cast<uint>(image.sizeInBytes());
auto const imageData = image.bits();
rgba = std::vector<uchar>(numberOfBytes,0);
rgbaReset = std::vector<uchar>(numberOfBytes,0);
for (uint i{0}; i < numberOfBytes; ++i) {
rgbaReset[i] = rgba[i] = imageData[i];
}
}
void MainWindow::rgbaInsertion()
{
auto *imageData = new uchar[numberOfBytes];
for (uint i{0};i < numberOfBytes;++i) {
imageData[i] = rgba[i];
}
image = QImage(imageData, image.width(), image.height(), QImage::Format_RGBA8888);
}
void MainWindow::on_importButton_clicked()
{
importImage();
scaleImage();
displayImage();
rgbaExtraction();
}
void MainWindow::on_quitButton_clicked()
{
QApplication::quit();
}
void MainWindow::sceneUpdater()
{
showImage->setPixmap(QPixmap::fromImage(image));
scene->update();
ui->view->update();
}
void MainWindow::on_redSlider_valueChanged(int value)
{
QString redString = QString::number(value);
ui->redLabel->setText(redString);
redDelta = value;
colorRed();
rgbaInsertion();
sceneUpdater();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QImage>
#include <QFileDialog>
#include <string>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_importButton_clicked();
void on_quitButton_clicked();
void on_redSlider_valueChanged(int value);
private:
QGraphicsPixmapItem *showImage;
QGraphicsScene *scene;
QString filter;
QImage image;
bool importCancelled;
QStatusBar *statusBar;
uint numberOfBytes;
std::vector<uchar> rgba;
std::vector<uchar> rgbaReset;
int redDelta{0};
int greenDelta{0};
int blueDelta{0};
int opacityDelta{0};
void importImage();
void scaleImage();
void displayImage();
void rgbaExtraction();
void rgbaInsertion();
void sceneUpdater();
void colorRed(); // Implemented in color.cpp
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
color.cpp
#include <mainwindow.h>
void MainWindow::colorRed()
{
for (uint i{0}; i < rgba.size()*sizeof (rgba[i]);i+=4) {
if(rgbaReset[i] + static_cast<uchar>(redDelta)>=255){
rgba[i] = 255;
}
else {
rgba[i] = rgbaReset[i];// + static_cast<uchar>(redDelta);
}
}
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
The problem is in scaleImage(), because scaledToHeight() returns another image format. You may omit the scale operation, or convert the returned image to Format_RGBA8888:
void MainWindow::scaleImage()
{
if (image.isNull()){
return;
}
else {
image = image.scaledToHeight(ui->view->height(), Qt::TransformationMode::SmoothTransformation)
.convertToFormat(QImage::Format_RGBA8888);
}
}
My recomendation is to add some instrumentation after each image manipulation to check that it has the expected format:
qDebug() << Q_FUNC_INFO << "image format:" << image.format();
I am looking for an easy way to link hand coded design to the UI for widgets applications in Qt. I plan to use the UI builder to easily adjust the layouts and obtain proper spacing, which I find hard to do without the UI builder.
I want to create a 3x3 grid of buttons for which I plan to use QVector< QVector<QPushButton*> > (I am not sure how I would do this in UI builder.)
Here is what I have tried, Why are the buttons not displayed even when I set the parent of each button to the widget ?
main.cpp
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPushButton>
namespace Ui {
class Window;
}
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
~Window();
private:
Ui::Window *ui;
static const int tiles = 50, height = 600, width = 500;
QVector< QVector<QPushButton*> > cells;
};
#endif // WINDOW_H
window.cpp
#include "window.h"
#include "ui_window.h"
Window::Window(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window)
{
ui->setupUi(this);
this->resize(width, height);
int j = 0;
for(auto &row : cells)
{
int i = 0;
for(auto &col : row)
{
col = new QPushButton(this);
col->setGeometry(i, j, tiles, tiles);
i += tiles;
}
j += tiles;
}
}
Window::~Window()
{
delete ui;
for(auto &row : cells)
{
for(auto &col : row)
{
delete col;
}
}
}
Any help would be appreciated.
The vectors are empty, so you iterate over nothing. Instead of managing these manually, you could leverage the grid layout.
Alas, you're complicating things unnecessarily with all the manual memory management and geometry management. It's unnecessary. All you need to do is to add widgets to the layout you allude to. And even then, I don't see how relegating the layout to a .ui file helps you since there the layout must be empty. So yes: you can set spacings, but you won't see them until you run the code. So it seems like a pointless exercise, unless you have other elements you're not telling us about (why aren't you - you went so far already).
Below is a minimum example that simplifies it as much as practicable, but see this answer and the links therein for more idea.
// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-43214317
#include <QtWidgets>
namespace Ui { class Window {
public:
// Approximate uic output
QGridLayout *layout;
void setupUi(QWidget * widget) {
layout = new QGridLayout(widget);
}
}; }
class Window : public QWidget
{
Q_OBJECT
Ui::Window ui;
QPushButton * buttonAt(int row, int column) {
auto item = ui.layout->itemAtPosition(row, column);
return item ? qobject_cast<QPushButton*>(item->widget()) : nullptr;
}
public:
explicit Window(QWidget *parent = {});
};
Window::Window(QWidget *parent) : QWidget(parent) {
ui.setupUi(this);
for (int i = 0; i < 5; ++i)
for (int j = 0; j < 6; ++j)
{
auto b = new QPushButton(QStringLiteral("%1,%2").arg(i).arg(j));
ui.layout->addWidget(b, i, j);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
#include "main.moc"
I have a set of QImages which come to a function (after a fixed interval of 4 seconds) and the function's job is to update the QLabel to show the new image.
While doing this, I can see a very obvious delay in the image rendering.
I had also followed the suggestions on the link:
Efficient way of displaying a continuous stream of QImages
But, even with using ImageDisplay in the link above, I can see a delay in image rendering.
Can anyone please suggest the best way to do this?
Below is the code.. The images required for the code to run are located at:
https://www.dropbox.com/sh/jiqdfqoiimjs7ei/AAAXezUeeCFyZXjNNOTmWZVga?dl=0
#include <QDialog>
#include <QtGui>
#include <QtCore>
#include <QApplication>
#include <QWidget>
#include <QImage>
class imageDisplay : public QWidget
{
public:
imageDisplay(QWidget*);
~imageDisplay();
void setImage(QImage* img);
private:
QImage* m_image;
protected:
void paintEvent(QPaintEvent* evt);
};
imageDisplay::imageDisplay(QWidget* parent) : QWidget(parent)
{
m_image = 0;
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
}
imageDisplay::~imageDisplay()
{
}
void imageDisplay::setImage(QImage* img)
{
m_image = img;
repaint();
}
void imageDisplay::paintEvent(QPaintEvent*)
{
if(!m_image) return;
QPainter painter(this);
painter.drawImage(rect(), *m_image, m_image->rect());
}
////////////////////////////////////
//
int main(int arc, char ** argv)
{
QApplication theApp(arc, argv, true);
QDialog* dlg = new QDialog();
imageDisplay* wgt = new imageDisplay(dlg);
wgt->resize(600,400);
dlg->show();
for(int i = 0 ; i <= 19; ++i)
{
sleep(1);
QString fileName = "aaa" + QString::number(i) + ".png";
QImage* img = new QImage(fileName);
wgt->setImage(img);
}
return theApp.exec();
}
I basically just started using Gnuplot and in trying to set xrange/yrange I am not able to find out how to set variable bounds.
For example : instance << "set yrange [-10:10]" works perfectly
but instance << "set yrange [yrange1:yrange2] doesn't.
yrange1, yrange2 are variables in which user input is stored. That way the user can decide the bounds for gnuplot
Is there a simple way to do this?
Edit : Sorry.Here is the code
qtgnuplotlib-example.cpp
//#include <QApplication>
#include "visualization.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
Visualization v;
v.show();
return app.exec();
}
visualization.h
#ifndef VISUALIZATION_H
#define VISUALIZATION_H
#include <QMainWindow>
#include <QtGnuplot/QtGnuplotWidget.h>
#include <QtGnuplot/QtGnuplotInstance.h>
namespace Ui {
class Visualization;
}
class Visualization : public QMainWindow
{
Q_OBJECT
public:
explicit Visualization(QWidget *parent = 0);
~Visualization();
private:
QtGnuplotWidget *widget;
QtGnuplotInstance *instance;
protected:
bool eventFilter(QObject *obj, QEvent *event);
private slots:
void on_actionExit_triggered();
void on_actionOpen_triggered();
void on_pushButton_clicked();
private:
Ui::Visualization *ui;
};
#endif // VISUALIZATION_H
visualization.cpp
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
#include <QString>
#include "visualization.h"
#include "ui_visualization.h"
using namespace std;
Visualization::Visualization(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Visualization)
{
ui->setupUi(this);
widget = new QtGnuplotWidget();
widget->installEventFilter(this);
widget->setStatusLabelActive(true);
instance = new QtGnuplotInstance();
instance->setWidget(widget);
}
Visualization::~Visualization()
{
delete ui;
delete instance;
delete widget;
}
void Visualization::on_actionExit_triggered()
{
qApp->quit();
}
void Visualization::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
tr("All Files (*.*)"));
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
return;
}
QTextStream in(&file);
QString str = in.readAll();
ui->equationsTxt->setText(str);
file.close();
}
}
bool Visualization::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress)
{
if (obj == this->widget) {
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton) {
ui->outputTxt->setText( this->widget->getStatusLabel()->text());
}
}
}
return QObject::eventFilter(obj, event);
}
void Visualization::on_pushButton_clicked()
{
widget->show();
widget->resize(QSize(800,600));
int yrange1 = 5;
int yrange2 = 10;
// *instance <<\
//"set yrange [-1.5:1.5]\nset xrange [-1.5:1.5]\nset isosamples 500,500\nf(x,y)= x**2+y**2-1\nset contour\nset cntrparam levels discrete 0\nset view 0,0\nunset ztics\nunset surface\nsplot f(x,y)\n";
//here is where I am stuck
*instance << "set tics scale 0.75\nset xtics 1\nset ytics 1\nset yrange [-10:10]\nset xlabel 'x'\nset ylabel 'y'\nset zeroaxis\nplot \"<echo '1 2'\" notitle\n";
}
The QtGnuplotInstance class provides an exec method which you can pass a QByteArray with commands:
QtGnuplotInstance instance = new QtGnuplotInstance();
QString command;
QTextStream(&command) << "set yrange [" << yrange1 << ":" << yrange2 << "]";
instance.exec(command.toLocal8Bit());
I cannot test this at the moment, but the direction should be correct.