PDA

View Full Version : how to create column headers by selecting radiobuttons and checkboxes



pkjag
10th September 2013, 17:43
Hi all
So i was wondering how one would go about setting the column fields of a table by selecting a radio button or checkbox.

something like:
selecting checkme


QCheckBox *checkmeCheckBox = new QCheckBox(this, tr("checkme"));

will create a column header with the same name checkme so that my table will have new columns with the same names as the radiobuttons and checkboxes i set checked

thanks in advance.

studentQt
10th September 2013, 19:01
Hi , I wrote some codes
opened a project which it's base class is QMainWindow

added to .h


private slots:
void addColumn();
void deleteColumn();

added to mainwindow.cpp


connect(ui->chkBox_add,SIGNAL(clicked()),this,SLOT(addColumn() ));
connect(ui->chkBox_delete,SIGNAL(clicked()),this,SLOT(deleteCo lumn()));


void MainWindow::addColumn()
{
ui->chkBox_add->setChecked(false);
ui->tableWidget->setColumnCount(ui->tableWidget->columnCount()+1);
}

void MainWindow::deleteColumn()
{
ui->chkBox_delete->setChecked(false);
ui->tableWidget->setColumnCount(ui->tableWidget->columnCount()-1);
}

pkjag
10th September 2013, 19:59
Hi and thanks for your reply
So why did you setChecked(false)?
Say you have the checkboxes in a wizard and after the wizard setup wraps up it stores the checkboxes' history and uses that to create the.columns, what would you change in your code to integrate that?
Also since your checkbox name is chkBox_add, what should be done so that my column has the same name?

studentQt
11th September 2013, 00:11
sorry , I couldn't understand what u want to say exactly

I try to reply as far as I understand

I create the chkBox_add and chkBox_delete chechBoxes in .ui file

then I'll try to make same name the new adding columns

if I find a way , I write here

the .ui file display below

-------------------------------
setChecked(false) , that's just my choice , I thought it's more good with that feature

-------------------------------

u can make same name the column in this way



QTableWidgetItem *item = new QTableWidgetItem(QString(""),QTableWidgetItem::Type);
// or QTableWidgetItem *item = new QTableWidgetItem(" ",QTableWidgetItem::Type);
ui->tableWidget->setHorizontalHeaderItem(column, item); // don't forget the column array starts from 0

for example if u define the item in that way u copy the names


QTableWidgetItem *item = new QTableWidgetItem(ui->tablewidget->horizontalHeaderItem(column-1)->text(),QTableWidgetItem::Type);
ui->tableWidget->setHorizontalHeaderItem(column, item);

pkjag
11th September 2013, 07:58
What i'm trying to say is. I have a wizard that i created separately and also a mainwindow that's supposed to launch a table once you click finish in the wizard. So in the wizard i have a couple of checkboxes and radiobuttons, when i click them so that they stay checked i want that to generate the columns of my table in mainwindow with the same names as the checkboxed and radiobuttons i set checked?
Is it possible to use your code for that?

studentQt
11th September 2013, 10:55
I wrote somes , hope it's your answer what u want

and it works good , I'll add them end of the reply

But the place I don't understand is how u can see a cople of checkboxes and radiobuttons to create a project which is base class is QMainWindow in the wizard

pls can u take screen shots or take a video

.pro file


#-------------------------------------------------
#
# Project created by QtCreator 2013-09-10T20:45:39
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = deneme
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui


.h file


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidgetItem>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:
void addColumn();
};

#endif // MAINWINDOW_H



mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->chkBox_add,SIGNAL(clicked()),this,SLOT(addColumn() ));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::addColumn()
{
int column;
column = ui->tableWidget->columnCount(); // now column = 0 because there isn't any columns
QTableWidgetItem *item = new QTableWidgetItem(QString(ui->lnEdt_AdColumnName->text()),QTableWidgetItem::Type);

ui->tableWidget->setColumnCount(column+1); // addin new column
if(ui->lnEdt_AdColumnName->text().isEmpty() == false)
ui->tableWidget->setHorizontalHeaderItem(column, item);
else {
item->setText(ui->tableWidget->horizontalHeaderItem(column-1)->text());
ui->tableWidget->setHorizontalHeaderItem(column, item); // the column array starts from 0
}

ui->chkBox_add->setChecked(false);
ui->lnEdt_AdColumnName->setText("");
}


.ui file edit mode


<?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>566</width>
<height>350</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>551</width>
<height>271</height>
</rect>
</property>
</widget>
<widget class="QCheckBox" name="chkBox_add">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>141</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>add column</string>
</property>
</widget>
<widget class="QLineEdit" name="lnEdt_AdColumnName">
<property name="geometry">
<rect>
<x>130</x>
<y>10</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>566</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>



.ui file design mode

9570

I wrote all of them because of I don't understand your checkbox and radiobutton event exactly

pkjag
11th September 2013, 13:02
So this is part of my wizard:

9573

and i want all the checked checkboxes and radiobuttons to appear as columns with the same names as the checkboxes in my mainwindow which is launched after i click the finish button in the last page of the wizard.
This is the last page of the wizard:

9574

And this is the mainwindow where the table is supposed to run once i click finish in the last page:

9575

hope you can help??

studentQt
11th September 2013, 14:55
Probably , u want a thing like that way

u'll click to a button then the program'll check the all checkboxes

-------------------------
there is a thing u should now : radiobuttons are different from checkboxes
After u click a radiobutton , if u click another radiobutton , checking feature of the radiobutton which it's first chechked goes unchecked

-------------------------

I'll add a video about that thing what I uderstand

pkjag
11th September 2013, 15:59
Yeah i know enough about radio buttons and checkboxes, so is there anyway you can help?

studentQt
11th September 2013, 16:33
I wrote a program about checkboxes


http://www.youtube.com/watch?v=yv9iTN2dBq4&feature=youtu.be

pkjag
11th September 2013, 16:56
Could you post the code snippets here cause i cannot play the video at this moment cause i have some problems with the video plugins in my browser??

studentQt
11th September 2013, 17:01
this is mainwindow.cpp content



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
QString name = ui->checkBox->text();
ui->checkBox->setChecked(true);
if(ui->checkBox_2->text() == name) ui->checkBox_2->setChecked(true);
if(ui->checkBox_3->text() == name) ui->checkBox_3->setChecked(true);
}



other files are same (mainwindow.h,main.cpp and .pro files)

.ui file appearance

9578

pkjag
11th September 2013, 17:10
But how does that relate to creating columns with the same name as the checkboxes??

studentQt
11th September 2013, 17:18
sorry , I'll try on it

wysota
13th September 2013, 07:50
What's exactly the issue here? It seems to me the problem is trivial -- iterate over marked checkboxes and add columns to a table with names corresponding to checkboxes.

studentQt
14th September 2013, 01:50
sorry , did I do a mistake?

studentQt
14th September 2013, 16:14
:) that's not about this issue
I'm very surprised when I see that :)