i have Qtablewidget i have implemented a delegate for that table
like so : Num(default) | item(Qcombobox) | -qty-(QdoubleSpinBox)| Price(QdoubleSpinBox)|subtotal (QdoubleSpinBox)
i want to calculate subtotal( qty * price ) for each row using delegate, but really i don't know how to emit data from delegate
i tried this from my custom delegate
QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::createEditor(parent,option,index);
int col= index.column();
if(col == 1)
{
QComboBox *comboboxEditor = new QComboBox(parent);
return comboboxEditor;
}
// col3
else if(col ==2 || col ==3 || col ==4 || col == 5 || col == 6 || col == 7)
{
QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
doubleSpinBoxEditor->setInputMethodHints(Qt::ImhFormattedNumbersOnly);
doubleSpinBoxEditor->setRange(0.000000,999999999);
doubleSpinBoxEditor->setSuffix(" D.A");
doubleSpinBoxEditor->setDecimals(2);
doubleSpinBoxEditor->setFrame(false);
if(col == 2 || col == 3)
{
connect(doubleSpinBoxEditor,SIGNAL(valueChanged(double)),this,SLOT(testSlot(double)));
}
return doubleSpinBoxEditor;
}else{
return QStyledItemDelegate::createEditor(parent,option,index);
}
}
so , what's the best way to do this ?
after thinking i get the solution her is the code
Note: this is depand in my logic , you can change it as you want
QTableWidgetItem *item= NULL;
for(int row=0; row < ui->tableWidget->rowCount(); row++)
{
for (int col=0; col< ui->tableWidget->columnCount(); col++)
{
item = new QTableWidgetItem;
ui->tableWidget->setItem(row,col,item);
}
}
for(int row =0; row < ui->tableWidget->rowCount(); row++)
{
for(int col=0; col < ui->tableWidget->columnCount(); col++)
{
if(row != 0)
ui->tableWidget->item(row,col)->setFlags(Qt::NoItemFlags);
else{
if(col == 0 || col == 4 || col == 5 || col == 7 || col == 6)
ui->tableWidget->item(row,col)->setFlags(Qt::NoItemFlags);
}
}
}
Related
So I have two identical files representing a matrix :
4x3
1 2 3
1 2 3
1 2 3
1 2 3
I have to sum the two matrix , but I have to use processe in which each process is responsible for calculating each column of the matrix.
I have this :
int addmx(const char *file1, const char *file2){
FILE *f1 = fopen(file1,"r");
if (f1 == NULL){
perror("Error in opening file 1");
return -1;
}
FILE *f2 = fopen (file2, "r");
if (f2 == NULL){
perror("Error in opening file 2");
return -1;
}
struct stat sb;
if (fstat(fileno(f1),&sb) == -1){
perror("Couldn't get file size.\n");
}
printf("The file size is %ld\n",sb.st_size);
struct stat sb2;
if (fstat(fileno(f2),&sb2) == -1){
perror("Couldn't get file size.\n");
}
printf("The file size is %ld\n",sb2.st_size);
char *file_in_memory = mmap(NULL,sb.st_size ,PROT_READ ,MAP_SHARED,fileno(f1),0);
char *file_in_memory2 = mmap(file_in_memory,sb2.st_size,PROT_READ,MAP_SHARED,fileno(f2),0);
char *file_in_memory3 = mmap(file_in_memory2,sb.st_size ,PROT_WRITE,MAP_SHARED,0,0);
for (int i = 0; i < sb.st_size;i++){
printf("%c",file_in_memory[i]);
}
printf("\n");
for (int i = 0; i < sb2.st_size;i++){
printf("%c",file_in_memory2[i]);
}
int counter = 1;
char ch1;
do {
ch1 = fgetc(f1);
if (ch1 == '\n'){
break;
}
else counter++;
} while(1);
unsigned int row = atoi(&file_in_memory[0]) ;
unsigned int col = atoi(&file_in_memory[2]) ;
//file_in_memory3[0] = row + '0'; file_in_memory3[1] = 'x'; file_in_memory3[2] = col + '0';
pid_t pid_matrix[col];
for (size_t i = 1; i <= col;i++) {
pid_matrix[col] = fork();
if (pid_matrix[col] == -1) {
//erro
}
else if (pid_matrix[col] == 0) {
//child process
for (int j = 1; j <= row;j++){
int new_col = atoi(&file_in_memory[counter + (j-1)*(col*2) + ((i-1)*2)] )+ atoi(&file_in_memory2[counter + (j-1)*(col*2) + ((i-1)*2)]);
file_in_memory3[counter+ (j-1)*(col*2) + ((i-1)*2)] = new_col + '0';
printf("%d\n",new_col);
}
printf("Child process with pid %d for column %d\n", getpid(),i);
exit(EXIT_SUCCESS);
}
else {
//parent process
printf("Created a new process with pid %d\n", pid_matrix[col]);
}
}
for ( int i = 0; i < sb.st_size;i++){
printf("%c",file_in_memory3[i]);
}
munmap(file_in_memory,sb.st_size);
munmap(file_in_memory2,sb2.st_size);
close(f1);
close(f2);
return 0;
}
However, the child processes are not executing ( because it is not printing "Child process with pid ... ") but when I remove the line of code :
"file_in_memory3[counter + (j-1)(col2) + ((i-1)*2)] = new_col + '0';"
the child processes actually execute...
I'm trying to program a code decoder. But I get the following error for all the comparisons in the if statements:
'error: ISO C++ forbids comparison between pointer and integer
[-fpermissive]'
The examples for the input string are ".-.--" and "-..-.--".
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int c[100], t = 0, l, i = 0;
l = s.length();
cin >> s;
if (s[0] == '.') {
c[0] = 0;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '.') {
c[0] = 1;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.'
'){
c[t] = 1; t += 1; i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '-') {
c[0] = 2;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == ".") {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == "-" && s[i + 1] == "-") {
c[t] = 2;
t += 1;
i += 2;
}
}
}
for (i = 0; i < t; i++) {
cout << s[t];
}
return 0;
}
How do I resolve this issue?
You were using single quotes until you got here:
if(s[i]=="-"&&s[i+1]=="-"){
You need to change it to single quotes so you have an int to int comparison.
if(s[i]=='-'&&s[i+1]=='-'){
When you say
"-"
you are creating a pointer.
When you say
'='
you are creating an int.
(" ") is a string literal which is char const * which is a pointer and (' ') is char which get promoted to int, so you can't compare them. They must be type compatible.
I want to create a table, my code in QtCreator:
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableWidget->setColumnCount(5);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("First") << tr("Second") << tr("Third") << tr("Fourth") << tr("Fifth"));
ui->tableWidget->setColumnWidth(0, 80);
ui->tableWidget->setColumnWidth(1, 140);
ui->tableWidget->setColumnWidth(2, 80);
ui->tableWidget->setColumnWidth(3, 140);
ui->tableWidget->setStyleSheet("QHeaderView::section { background-color: rgb(217,217,217)}");
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
for (int c = 0; c < ui->tableWidget->horizontalHeader()->count(); c++) {
ui->tableWidget->horizontalHeaderItem(c)->setSizeHint(QSize(0, 50));
}
for (int j = 0; j < ui->tableWidget->columnCount(); j++) {
QTableWidgetItem *item = new QTableWidgetItem;
if ((j == 0) || (j == 3) || (j == 4)) {
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
}
if (j!=0) {
item->setTextAlignment(Qt::AlignCenter);
}
ui->tableWidget->setItem(0, j, item);
}
There is only one row in the table, I do it with Designer, so there is no line for the number of row in my code above.
Result:
With the same code above, I do in Visual Studio and receive this table:
The height of row with VS is the height of the table, which is too large. How can I receive the small row as in QtCreator ?
Example:
Given “25525511135”
Output : [“255.255.11.135”, “255.255.111.35”]. (sorted order)
Kindly let me know if we could do a depth first search over here ?(that's the only thing striking me )
Why is it important to have an 'optimal' approach for answering this?
There are not many permutations so the simple approach of checking every combination that fits into the IP format and then filtering out those that have out of range numbers will easily work.
It's unlikely to be a bottle neck for whatever this is part of.
You probably want a dynamic programming algorithm for the general case (something like
http://www.geeksforgeeks.org/dynamic-programming-set-32-word-break-problem/).
Instead of testing whether prefixes can be segmented into words in the dictionary, you'd be testing to see whether the prefixes are prefixes of some valid IPv4 address.
Brutal DFS is acceptable in this problem:
class Solution{
private:
vector<string> ans;
int len;
string cur, rec, str;
bool IsOk(string s) {
if(s[0] == '0' && s.size() > 1) return false;
int sum = 0;
for(int i = 0; i < s.size(); i ++) {
if(s[i] == '.') return false;
sum = sum * 10 + s[i] - '0';
}
if(sum >= 0 && sum <= 255) return true;
return false;
}
void dfs(int x, int cnt) {
if(x == len) {
if(str.size() != len + 4) return ;
string tmp(str);
tmp.erase(tmp.size() - 1, 1);
if(cnt == 4) ans.push_back(tmp);
return ;
}
if(cnt > 4 || str.size() > len + 4) return ;
string tmp = cur;
cur += rec[x];
if(!IsOk(cur)) {
cur = tmp;
return ;
}
dfs(x + 1, cnt);
string tmp2 = cur + '.';
str += tmp2;
cur = "";
dfs(x + 1, cnt + 1);
str.erase(str.size() - tmp2.size(), tmp2.size());
cur = tmp;
}
public:
vector<string> restoreIpAddresses(string s) {
this->len = s.size();
this->rec = s;
cur = str = "";
ans.clear();
dfs(0, 0);
return ans;
}
};
Here is a recursive solution on JavaScript. The result is not sorted.
// Task from https://www.geeksforgeeks.org/program-generate-possible-valid-ip-addresses-given-string/
// Given a string containing only digits, restore it by returning all possible valid IP address combinations.
//
// Example:
// Input : 25525511135
// Output : [“255.255.11.135”, “255.255.111.35”]
//
(function () {
function getValidIP(str) {
const result = [];
const length = str.length;
check(0, 0, '');
function check(start, level, previous){
let i = 0;
let num;
if (level === 3) {
num = str.substring(start);
if (num && num < 256) {
result.push(`${previous}.${num}`);
}
return;
}
num = str.substring(start, start + 1);
if (num == 0) {
check(start + 1, level + 1, level === 0 ? `${num}`: `${previous}.${num}`);
} else {
while (num.length < 4 && num < 256 && start + i + 1 < length) {
check(start + i + 1, level + 1, level === 0 ? `${num}`: `${previous}.${num}`);
i++;
num = str.substring(start, start + i + 1);
}
}
}
return result;
}
console.log('12345:')
console.time('1-1');
console.log(getValidIP('12345'));
console.timeEnd('1-1');
console.log('1234:')
console.time('1-2');
console.log(getValidIP('1234'));
console.timeEnd('1-2');
console.log('2555011135:')
console.time('1-3');
console.log(getValidIP('2555011135'));
console.timeEnd('1-3');
console.log('222011135:')
console.time('1-4');
console.log(getValidIP('222011135'));
console.timeEnd('1-4');
})();
I'm trying to solve this question: http://www.spoj.com/problems/ALLIZWEL/
Find whether there is a path in the given matrix which makes the
sentence “ALL IZZ WELL”.
There is a path from any cell to all its neighbouring cells.
A neighbour may share an edge or a corner.
Input Specification:
The first line consists of an integer t representing the number of test cases.
The first line of each test
case consists of two integers R and C representing the number of rows and number of columns in the matrix.
Output Specification:
For each test case print “YES” if there is a path which makes the sentence “ALLIZZWELL”.
Else print “NO”.
For sample test cases, open the link.
My code:
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <stack>
#include <queue>
#include <climits>
#include <set>
using namespace std;
char matrix[101][101];
bool var;
int r,c;
bool check (string str,int pos, bool visited[101][101],int i, int j);
int main (void)
{
int t,i,j;
cin>>t;
bool ans;
while (t != 0)
{
int r,c,flag=0;
cin>>r>>c;
for ( i = 0; i < r; i++ )
{
for ( j = 0; j < c; j++ )
{
cin>>matrix[i][j];
}
}
string str = "ALLIZZWELL";
int pos = 1;
for ( i = 0; i < r; i++ )
{
for ( j = 0; j < c; j++ )
{
bool visited[101][101];
for ( i = 0; i < 101; i++ )
for ( j = 0; j < 101; j++ )
visited[i][j] = false;
visited[i][j] = true;
if (matrix[i][j] == 'A') // for all possible starting positions
ans = check(str,pos,visited,i,j);
if (ans == true)
{
cout<<"YES\n";
flag = 1;
break;
}
if (flag == 1)
break;
}
}
if (flag == 0)
cout<<"NO\n";
t--;
}
return 0;
}
bool check (string str,int pos, bool visited[101][101],int i, int j) // checking for all possible test cases
{
bool result = false;
if (pos == str.length() + 1)
return true;
if (i+1 < r && visited[i+1][j] != true && matrix[i+1][j] == str[pos])
{
visited[i+1][j] = true;
result = result || check(str,pos+1,visited,i+1,j);
if (result == false)
visited[i+1][j] = false;
}
else if (i-1 >= 0 && visited[i-1][j] != true && matrix[i-1][j] == str[pos])
{
visited[i-1][j] = true;
result = result || check(str,pos+1,visited,i-1,j);
if (result == false)
visited[i-1][j] = true;
}
else if (j+1 < c && visited[i][j+1] != true && matrix[i][j+1] == str[pos])
{
visited[i][j+1] = true;
result = result || check(str,pos+1,visited,i,j+1);
if (result == false)
visited[i][j+1] = true;
}
else if (j-1 >= 0 && visited[i][j-1] != true && matrix[i][j-1] == str[pos])
{
visited[i][j-1] = true;
result = result || check(str,pos+1,visited,i,j-1);
if (result == false)
visited[i][j-1] = true;
}
else if (i+1 < r && j+1 < c && visited[i+1][j+1] != true && matrix[i+1][j+1] == str[pos])
{
visited[i+1][j+1] = true;
result = result || check(str,pos+1,visited,i+1,j+1);
if (result == false)
visited[i+1][j+1] = true;
}
else if (i+1 < r && j-1 >= 0 && visited[i+1][j-1] != true && matrix[i+1][j-1] == str[pos])
{
visited[i+1][j-1] = true;
result = result || check(str,pos+1,visited,i+1,j-1);
if (result == false)
visited[i+1][j-1] = true;
}
else if (i-1 >= 0 && j+1 < c && visited[i-1][j+1] != true && matrix[i-1][j+1] == str[pos])
{
visited[i-1][j+1] = true;
result = result || check(str,pos+1,visited,i-1,j+1);
if (result == false)
visited[i-1][j+1] = true;
}
else if (i-1 >= 0 && j-1 >= 0 && visited[i-1][j-1]!= true && matrix[i-1][j-1] == str[pos])
{
visited[i-1][j-1] = true;
result = result || check(str,pos+1,visited,i-1,j-1);
if (result == false)
visited[i-1][j-1] = true;
}
return false;
}
The code is quite self-explanatory: I am trying all possible cases.
I am getting a WA in the third test case, i.e.
2 9
A.L.Z.E..
.L.I.W.L.
I tried debugging but I couldn't narrow down my problem.
The main problems are:
Using i and j in the loop clearing visited (as well as the outer loops)
Using r and c as global variables in check, but writing them as local variables in main
Always returning false from check (instead of result)
Only trying the first choice in check (turn "else if" into "if")
Not clearing the value in ans
Only breaking out of the inner loop, not both the i and j loop
Terminating the search when pos gets to str.length()+1 instead of str.length()
It often helps to put some print statements in recursive functions like these, try them out on a simple example, and see whether the sequence of calls matches your expectations.